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(RTE_MACHINE_CPUFLAG_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 uint32_t tb_credits_per_period;
56 /* Pipe traffic classes */
58 uint32_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 uint32_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 uint32_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_grinder {
106 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
107 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
112 enum grinder_state state;
115 struct rte_sched_subport *subport;
116 struct rte_sched_pipe *pipe;
117 struct rte_sched_pipe_profile *pipe_params;
120 uint8_t tccache_qmask[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
121 uint32_t tccache_qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
127 struct rte_sched_queue *queue[RTE_SCHED_MAX_QUEUES_PER_TC];
128 struct rte_mbuf **qbase[RTE_SCHED_MAX_QUEUES_PER_TC];
129 uint32_t qindex[RTE_SCHED_MAX_QUEUES_PER_TC];
133 struct rte_mbuf *pkt;
136 uint16_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
137 uint16_t wrr_mask[RTE_SCHED_BE_QUEUES_PER_PIPE];
138 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
141 struct rte_sched_subport {
142 /* Token bucket (TB) */
143 uint64_t tb_time; /* time of last update */
145 uint32_t tb_credits_per_period;
149 /* Traffic classes (TCs) */
150 uint64_t tc_time; /* time of next update */
151 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
152 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
155 /* TC oversubscription */
157 uint32_t tc_ov_wm_min;
158 uint32_t tc_ov_wm_max;
159 uint8_t tc_ov_period_id;
165 struct rte_sched_subport_stats stats;
168 uint32_t n_pipes_per_subport_enabled;
169 uint32_t n_pipe_profiles;
170 uint32_t n_max_pipe_profiles;
172 /* Pipe best-effort TC rate */
173 uint32_t pipe_tc_be_rate_max;
175 /* Pipe queues size */
176 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
179 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
182 /* Scheduling loop detection */
184 uint32_t pipe_exhaustion;
187 struct rte_bitmap *bmp;
188 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
191 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
192 uint32_t busy_grinders;
194 /* Queue base calculation */
195 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
198 struct rte_sched_pipe *pipe;
199 struct rte_sched_queue *queue;
200 struct rte_sched_queue_extra *queue_extra;
201 struct rte_sched_pipe_profile *pipe_profiles;
203 struct rte_mbuf **queue_array;
204 uint8_t memory[0] __rte_cache_aligned;
205 } __rte_cache_aligned;
207 struct rte_sched_port {
208 /* User parameters */
209 uint32_t n_subports_per_port;
210 uint32_t n_pipes_per_subport;
211 uint32_t n_pipes_per_subport_log2;
212 uint16_t pipe_queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
213 uint8_t pipe_tc[RTE_SCHED_QUEUES_PER_PIPE];
214 uint8_t tc_queue[RTE_SCHED_QUEUES_PER_PIPE];
217 uint32_t frame_overhead;
219 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
220 uint32_t n_pipe_profiles;
221 uint32_t n_max_pipe_profiles;
222 uint32_t pipe_tc_be_rate_max;
224 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
228 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cyles */
229 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
230 uint64_t time; /* Current NIC TX time measured in bytes */
231 struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
233 /* Scheduling loop detection */
235 uint32_t pipe_exhaustion;
238 struct rte_bitmap *bmp;
239 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
242 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
243 uint32_t busy_grinders;
244 struct rte_mbuf **pkts_out;
247 /* Queue base calculation */
248 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
251 /* Large data structures */
252 struct rte_sched_subport *subports[0];
253 struct rte_sched_subport *subport;
254 struct rte_sched_pipe *pipe;
255 struct rte_sched_queue *queue;
256 struct rte_sched_queue_extra *queue_extra;
257 struct rte_sched_pipe_profile *pipe_profiles;
259 struct rte_mbuf **queue_array;
260 uint8_t memory[0] __rte_cache_aligned;
261 } __rte_cache_aligned;
263 enum rte_sched_port_array {
264 e_RTE_SCHED_PORT_ARRAY_SUBPORT = 0,
265 e_RTE_SCHED_PORT_ARRAY_PIPE,
266 e_RTE_SCHED_PORT_ARRAY_QUEUE,
267 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA,
268 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES,
269 e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY,
270 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY,
271 e_RTE_SCHED_PORT_ARRAY_TOTAL,
274 enum rte_sched_subport_array {
275 e_RTE_SCHED_SUBPORT_ARRAY_PIPE = 0,
276 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE,
277 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA,
278 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES,
279 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY,
280 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY,
281 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL,
284 static inline uint32_t
285 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
287 return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
290 static inline struct rte_mbuf **
291 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
293 uint32_t pindex = qindex >> 4;
294 uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
296 return (subport->queue_array + pindex *
297 subport->qsize_sum + subport->qsize_add[qpos]);
300 static inline uint16_t
301 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
302 struct rte_sched_subport *subport, uint32_t qindex)
304 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
306 return subport->qsize[tc];
309 static inline uint32_t
310 rte_sched_port_queues_per_port(struct rte_sched_port *port)
312 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport * port->n_subports_per_port;
315 static inline struct rte_mbuf **
316 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
318 uint32_t pindex = qindex >> 4;
319 uint32_t qpos = qindex & 0xF;
321 return (port->queue_array + pindex *
322 port->qsize_sum + port->qsize_add[qpos]);
325 static inline uint16_t
326 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
328 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
330 return port->qsize[tc];
333 static inline uint16_t
334 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
336 uint16_t pipe_queue = port->pipe_queue[traffic_class];
341 static inline uint8_t
342 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
344 uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
349 static inline uint8_t
350 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
352 uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
358 pipe_profile_check(struct rte_sched_pipe_params *params,
359 uint32_t rate, uint16_t *qsize)
363 /* Pipe parameters */
364 if (params == NULL) {
366 "%s: Incorrect value for parameter params\n", __func__);
370 /* TB rate: non-zero, not greater than port rate */
371 if (params->tb_rate == 0 ||
372 params->tb_rate > rate) {
374 "%s: Incorrect value for tb rate\n", __func__);
378 /* TB size: non-zero */
379 if (params->tb_size == 0) {
381 "%s: Incorrect value for tb size\n", __func__);
385 /* TC rate: non-zero if qsize non-zero, less than pipe rate */
386 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
387 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
388 (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
389 params->tc_rate[i] > params->tb_rate))) {
391 "%s: Incorrect value for qsize or tc_rate\n", __func__);
396 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
397 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
399 "%s: Incorrect value for be traffic class rate\n", __func__);
403 /* TC period: non-zero */
404 if (params->tc_period == 0) {
406 "%s: Incorrect value for tc period\n", __func__);
410 /* Best effort tc oversubscription weight: non-zero */
411 if (params->tc_ov_weight == 0) {
413 "%s: Incorrect value for tc ov weight\n", __func__);
417 /* Queue WRR weights: non-zero */
418 for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
419 if (params->wrr_weights[i] == 0) {
421 "%s: Incorrect value for wrr weight\n", __func__);
430 rte_sched_port_check_params(struct rte_sched_port_params *params)
432 if (params == NULL) {
434 "%s: Incorrect value for parameter params\n", __func__);
439 if (params->socket < 0) {
441 "%s: Incorrect value for socket id\n", __func__);
446 if (params->rate == 0) {
448 "%s: Incorrect value for rate\n", __func__);
453 if (params->mtu == 0) {
455 "%s: Incorrect value for mtu\n", __func__);
459 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
460 if (params->n_subports_per_port == 0 ||
461 params->n_subports_per_port > 1u << 16 ||
462 !rte_is_power_of_2(params->n_subports_per_port)) {
464 "%s: Incorrect value for number of subports\n", __func__);
468 /* n_pipes_per_subport: non-zero, power of 2 */
469 if (params->n_pipes_per_subport == 0 ||
470 !rte_is_power_of_2(params->n_pipes_per_subport)) {
472 "%s: Incorrect value for maximum pipes number\n", __func__);
480 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
481 enum rte_sched_subport_array array)
483 uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
484 uint32_t n_subport_pipe_queues =
485 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
487 uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
488 uint32_t size_queue =
489 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
490 uint32_t size_queue_extra
491 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
492 uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
493 sizeof(struct rte_sched_pipe_profile);
494 uint32_t size_bmp_array =
495 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
496 uint32_t size_per_pipe_queue_array, size_queue_array;
500 size_per_pipe_queue_array = 0;
501 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
502 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
503 size_per_pipe_queue_array +=
504 params->qsize[i] * sizeof(struct rte_mbuf *);
506 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
507 params->qsize[i] * sizeof(struct rte_mbuf *);
509 size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
513 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
515 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
517 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
519 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
521 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
523 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
525 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
527 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
529 if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
531 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
533 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
535 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
541 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
545 subport->qsize_add[0] = 0;
547 /* Strict prority traffic class */
548 for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
549 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
551 /* Best-effort traffic class */
552 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
553 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
554 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
555 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
556 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
557 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
558 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
559 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
560 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
562 subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
563 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
567 rte_sched_port_log_pipe_profile(struct rte_sched_subport *subport, uint32_t i)
569 struct rte_sched_pipe_profile *p = subport->pipe_profiles + i;
571 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
572 " Token bucket: period = %u, credits per period = %u, size = %u\n"
573 " Traffic classes: period = %u,\n"
574 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
575 " Best-effort traffic class oversubscription: weight = %hhu\n"
576 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
581 p->tb_credits_per_period,
584 /* Traffic classes */
586 p->tc_credits_per_period[0],
587 p->tc_credits_per_period[1],
588 p->tc_credits_per_period[2],
589 p->tc_credits_per_period[3],
590 p->tc_credits_per_period[4],
591 p->tc_credits_per_period[5],
592 p->tc_credits_per_period[6],
593 p->tc_credits_per_period[7],
594 p->tc_credits_per_period[8],
595 p->tc_credits_per_period[9],
596 p->tc_credits_per_period[10],
597 p->tc_credits_per_period[11],
598 p->tc_credits_per_period[12],
600 /* Best-effort traffic class oversubscription */
604 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
607 static inline uint64_t
608 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
610 uint64_t time = time_ms;
612 time = (time * rate) / 1000;
618 rte_sched_pipe_profile_convert(struct rte_sched_subport *subport,
619 struct rte_sched_pipe_params *src,
620 struct rte_sched_pipe_profile *dst,
623 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
624 uint32_t lcd1, lcd2, lcd;
628 if (src->tb_rate == rate) {
629 dst->tb_credits_per_period = 1;
632 double tb_rate = (double) src->tb_rate
634 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
636 rte_approx(tb_rate, d,
637 &dst->tb_credits_per_period, &dst->tb_period);
640 dst->tb_size = src->tb_size;
642 /* Traffic Classes */
643 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
646 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
647 if (subport->qsize[i])
648 dst->tc_credits_per_period[i]
649 = rte_sched_time_ms_to_bytes(src->tc_period,
652 dst->tc_ov_weight = src->tc_ov_weight;
655 wrr_cost[0] = src->wrr_weights[0];
656 wrr_cost[1] = src->wrr_weights[1];
657 wrr_cost[2] = src->wrr_weights[2];
658 wrr_cost[3] = src->wrr_weights[3];
660 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
661 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
662 lcd = rte_get_lcd(lcd1, lcd2);
664 wrr_cost[0] = lcd / wrr_cost[0];
665 wrr_cost[1] = lcd / wrr_cost[1];
666 wrr_cost[2] = lcd / wrr_cost[2];
667 wrr_cost[3] = lcd / wrr_cost[3];
669 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
670 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
671 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
672 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
676 rte_sched_subport_config_pipe_profile_table(struct rte_sched_subport *subport,
677 struct rte_sched_subport_params *params, uint32_t rate)
681 for (i = 0; i < subport->n_pipe_profiles; i++) {
682 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
683 struct rte_sched_pipe_profile *dst = subport->pipe_profiles + i;
685 rte_sched_pipe_profile_convert(subport, src, dst, rate);
686 rte_sched_port_log_pipe_profile(subport, i);
689 subport->pipe_tc_be_rate_max = 0;
690 for (i = 0; i < subport->n_pipe_profiles; i++) {
691 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
692 uint32_t pipe_tc_be_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
694 if (subport->pipe_tc_be_rate_max < pipe_tc_be_rate)
695 subport->pipe_tc_be_rate_max = pipe_tc_be_rate;
700 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
701 uint32_t n_max_pipes_per_subport,
706 /* Check user parameters */
707 if (params == NULL) {
709 "%s: Incorrect value for parameter params\n", __func__);
713 if (params->tb_rate == 0 || params->tb_rate > rate) {
715 "%s: Incorrect value for tb rate\n", __func__);
719 if (params->tb_size == 0) {
721 "%s: Incorrect value for tb size\n", __func__);
725 /* qsize: if non-zero, power of 2,
726 * no bigger than 32K (due to 16-bit read/write pointers)
728 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
729 uint16_t qsize = params->qsize[i];
731 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
733 "%s: Incorrect value for qsize\n", __func__);
738 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
739 uint32_t tc_rate = params->tc_rate[i];
740 uint16_t qsize = params->qsize[i];
742 if ((qsize == 0 && tc_rate != 0) ||
743 (qsize != 0 && tc_rate == 0) ||
744 (tc_rate > params->tb_rate)) {
746 "%s: Incorrect value for tc rate\n", __func__);
751 if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
752 params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
754 "%s: Incorrect qsize or tc rate(best effort)\n", __func__);
758 if (params->tc_period == 0) {
760 "%s: Incorrect value for tc period\n", __func__);
764 /* n_pipes_per_subport: non-zero, power of 2 */
765 if (params->n_pipes_per_subport_enabled == 0 ||
766 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
767 !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
769 "%s: Incorrect value for pipes number\n", __func__);
773 /* pipe_profiles and n_pipe_profiles */
774 if (params->pipe_profiles == NULL ||
775 params->n_pipe_profiles == 0 ||
776 params->n_max_pipe_profiles == 0 ||
777 params->n_pipe_profiles > params->n_max_pipe_profiles) {
779 "%s: Incorrect value for pipe profiles\n", __func__);
783 for (i = 0; i < params->n_pipe_profiles; i++) {
784 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
787 status = pipe_profile_check(p, rate, ¶ms->qsize[0]);
790 "%s: Pipe profile check failed(%d)\n", __func__, status);
799 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *port_params,
800 struct rte_sched_subport_params **subport_params)
802 uint32_t size0 = 0, size1 = 0, i;
805 status = rte_sched_port_check_params(port_params);
808 "%s: Port scheduler port params check failed (%d)\n",
814 for (i = 0; i < port_params->n_subports_per_port; i++) {
815 struct rte_sched_subport_params *sp = subport_params[i];
817 status = rte_sched_subport_check_params(sp,
818 port_params->n_pipes_per_subport,
822 "%s: Port scheduler subport params check failed (%d)\n",
829 size0 = sizeof(struct rte_sched_port);
831 for (i = 0; i < port_params->n_subports_per_port; i++) {
832 struct rte_sched_subport_params *sp = subport_params[i];
834 size1 += rte_sched_subport_get_array_base(sp,
835 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
838 return size0 + size1;
841 struct rte_sched_port *
842 rte_sched_port_config(struct rte_sched_port_params *params)
844 struct rte_sched_port *port = NULL;
845 uint32_t size0, size1;
846 uint32_t cycles_per_byte;
850 status = rte_sched_port_check_params(params);
853 "%s: Port scheduler params check failed (%d)\n",
858 size0 = sizeof(struct rte_sched_port);
859 size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
861 /* Allocate memory to store the data structures */
862 port = rte_zmalloc_socket("qos_params", size0 + size1, RTE_CACHE_LINE_SIZE,
865 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
870 /* User parameters */
871 port->n_subports_per_port = params->n_subports_per_port;
872 port->n_pipes_per_subport = params->n_pipes_per_subport;
873 port->n_pipes_per_subport_log2 =
874 __builtin_ctz(params->n_pipes_per_subport);
875 port->socket = params->socket;
877 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
878 port->pipe_queue[i] = i;
880 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
881 port->pipe_tc[i] = j;
883 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
887 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
888 port->tc_queue[i] = j;
890 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
893 port->rate = params->rate;
894 port->mtu = params->mtu + params->frame_overhead;
895 port->frame_overhead = params->frame_overhead;
898 port->time_cpu_cycles = rte_get_tsc_cycles();
899 port->time_cpu_bytes = 0;
902 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
904 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
907 port->pkts_out = NULL;
908 port->n_pkts_out = 0;
914 rte_sched_subport_free(struct rte_sched_port *port,
915 struct rte_sched_subport *subport)
917 uint32_t n_subport_pipe_queues;
923 n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
925 /* Free enqueued mbufs */
926 for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
927 struct rte_mbuf **mbufs =
928 rte_sched_subport_pipe_qbase(subport, qindex);
929 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
931 struct rte_sched_queue *queue = subport->queue + qindex;
932 uint16_t qr = queue->qr & (qsize - 1);
933 uint16_t qw = queue->qw & (qsize - 1);
935 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
936 rte_pktmbuf_free(mbufs[qr]);
940 rte_bitmap_free(subport->bmp);
944 rte_sched_port_free(struct rte_sched_port *port)
948 /* Check user parameters */
952 for (i = 0; i < port->n_subports_per_port; i++)
953 rte_sched_subport_free(port, port->subports[i]);
959 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
961 struct rte_sched_subport *s = port->subports[i];
963 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
964 " Token bucket: period = %u, credits per period = %u, size = %u\n"
965 " Traffic classes: period = %u\n"
966 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
967 " Best effort traffic class oversubscription: wm min = %u, wm max = %u\n",
972 s->tb_credits_per_period,
975 /* Traffic classes */
977 s->tc_credits_per_period[0],
978 s->tc_credits_per_period[1],
979 s->tc_credits_per_period[2],
980 s->tc_credits_per_period[3],
981 s->tc_credits_per_period[4],
982 s->tc_credits_per_period[5],
983 s->tc_credits_per_period[6],
984 s->tc_credits_per_period[7],
985 s->tc_credits_per_period[8],
986 s->tc_credits_per_period[9],
987 s->tc_credits_per_period[10],
988 s->tc_credits_per_period[11],
989 s->tc_credits_per_period[12],
991 /* Best effort traffic class oversubscription */
997 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1001 for (i = 0; i < n_subports; i++) {
1002 struct rte_sched_subport *subport = port->subports[i];
1004 rte_sched_subport_free(port, subport);
1011 rte_sched_subport_config(struct rte_sched_port *port,
1012 uint32_t subport_id,
1013 struct rte_sched_subport_params *params)
1015 struct rte_sched_subport *s = NULL;
1016 uint32_t n_subports = subport_id;
1017 uint32_t n_subport_pipe_queues, i;
1018 uint32_t size0, size1, bmp_mem_size;
1021 /* Check user parameters */
1024 "%s: Incorrect value for parameter port\n", __func__);
1028 if (subport_id >= port->n_subports_per_port) {
1030 "%s: Incorrect value for subport id\n", __func__);
1032 rte_sched_free_memory(port, n_subports);
1036 status = rte_sched_subport_check_params(params,
1037 port->n_pipes_per_subport,
1040 RTE_LOG(NOTICE, SCHED,
1041 "%s: Port scheduler params check failed (%d)\n",
1044 rte_sched_free_memory(port, n_subports);
1048 /* Determine the amount of memory to allocate */
1049 size0 = sizeof(struct rte_sched_subport);
1050 size1 = rte_sched_subport_get_array_base(params,
1051 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1053 /* Allocate memory to store the data structures */
1054 s = rte_zmalloc_socket("subport_params", size0 + size1,
1055 RTE_CACHE_LINE_SIZE, port->socket);
1058 "%s: Memory allocation fails\n", __func__);
1060 rte_sched_free_memory(port, n_subports);
1067 port->subports[subport_id] = s;
1069 /* Token Bucket (TB) */
1070 if (params->tb_rate == port->rate) {
1071 s->tb_credits_per_period = 1;
1074 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
1075 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
1077 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
1080 s->tb_size = params->tb_size;
1081 s->tb_time = port->time;
1082 s->tb_credits = s->tb_size / 2;
1084 /* Traffic Classes (TCs) */
1085 s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
1086 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1087 if (params->qsize[i])
1088 s->tc_credits_per_period[i]
1089 = rte_sched_time_ms_to_bytes(params->tc_period,
1090 params->tc_rate[i]);
1092 s->tc_time = port->time + s->tc_period;
1093 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1094 if (params->qsize[i])
1095 s->tc_credits[i] = s->tc_credits_per_period[i];
1097 /* compile time checks */
1098 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1099 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1100 (RTE_SCHED_PORT_N_GRINDERS - 1));
1102 /* User parameters */
1103 s->n_pipes_per_subport_enabled = params->n_pipes_per_subport_enabled;
1104 memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1105 s->n_pipe_profiles = params->n_pipe_profiles;
1106 s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1108 #ifdef RTE_SCHED_RED
1109 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1112 for (j = 0; j < RTE_COLORS; j++) {
1113 /* if min/max are both zero, then RED is disabled */
1114 if ((params->red_params[i][j].min_th |
1115 params->red_params[i][j].max_th) == 0) {
1119 if (rte_red_config_init(&s->red_config[i][j],
1120 params->red_params[i][j].wq_log2,
1121 params->red_params[i][j].min_th,
1122 params->red_params[i][j].max_th,
1123 params->red_params[i][j].maxp_inv) != 0) {
1124 rte_sched_free_memory(port, n_subports);
1126 RTE_LOG(NOTICE, SCHED,
1127 "%s: RED configuration init fails\n", __func__);
1134 /* Scheduling loop detection */
1135 s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1136 s->pipe_exhaustion = 0;
1139 s->busy_grinders = 0;
1141 /* Queue base calculation */
1142 rte_sched_subport_config_qsize(s);
1144 /* Large data structures */
1145 s->pipe = (struct rte_sched_pipe *)
1146 (s->memory + rte_sched_subport_get_array_base(params,
1147 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1148 s->queue = (struct rte_sched_queue *)
1149 (s->memory + rte_sched_subport_get_array_base(params,
1150 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1151 s->queue_extra = (struct rte_sched_queue_extra *)
1152 (s->memory + rte_sched_subport_get_array_base(params,
1153 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1154 s->pipe_profiles = (struct rte_sched_pipe_profile *)
1155 (s->memory + rte_sched_subport_get_array_base(params,
1156 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1157 s->bmp_array = s->memory + rte_sched_subport_get_array_base(params,
1158 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1159 s->queue_array = (struct rte_mbuf **)
1160 (s->memory + rte_sched_subport_get_array_base(params,
1161 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1163 /* Pipe profile table */
1164 rte_sched_subport_config_pipe_profile_table(s, params, port->rate);
1167 n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1168 bmp_mem_size = rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
1169 s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1171 if (s->bmp == NULL) {
1173 "%s: Subport bitmap init error\n", __func__);
1175 rte_sched_free_memory(port, n_subports);
1179 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1180 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1182 #ifdef RTE_SCHED_SUBPORT_TC_OV
1183 /* TC oversubscription */
1184 s->tc_ov_wm_min = port->mtu;
1185 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
1186 s->pipe_tc_be_rate_max);
1187 s->tc_ov_wm = s->tc_ov_wm_max;
1188 s->tc_ov_period_id = 0;
1194 rte_sched_port_log_subport_config(port, subport_id);
1200 rte_sched_pipe_config(struct rte_sched_port *port,
1201 uint32_t subport_id,
1203 int32_t pipe_profile)
1205 struct rte_sched_subport *s;
1206 struct rte_sched_pipe *p;
1207 struct rte_sched_pipe_profile *params;
1208 uint32_t n_subports = subport_id + 1;
1209 uint32_t deactivate, profile, i;
1211 /* Check user parameters */
1212 profile = (uint32_t) pipe_profile;
1213 deactivate = (pipe_profile < 0);
1217 "%s: Incorrect value for parameter port\n", __func__);
1221 if (subport_id >= port->n_subports_per_port) {
1223 "%s: Incorrect value for parameter subport id\n", __func__);
1225 rte_sched_free_memory(port, n_subports);
1229 s = port->subports[subport_id];
1230 if (pipe_id >= s->n_pipes_per_subport_enabled) {
1232 "%s: Incorrect value for parameter pipe id\n", __func__);
1234 rte_sched_free_memory(port, n_subports);
1238 if (!deactivate && profile >= s->n_pipe_profiles) {
1240 "%s: Incorrect value for parameter pipe profile\n", __func__);
1242 rte_sched_free_memory(port, n_subports);
1246 /* Handle the case when pipe already has a valid configuration */
1247 p = s->pipe + pipe_id;
1249 params = s->pipe_profiles + p->profile;
1251 double subport_tc_be_rate =
1252 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1253 / (double) s->tc_period;
1254 double pipe_tc_be_rate =
1255 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1256 / (double) params->tc_period;
1257 uint32_t tc_be_ov = s->tc_ov;
1259 /* Unplug pipe from its subport */
1260 s->tc_ov_n -= params->tc_ov_weight;
1261 s->tc_ov_rate -= pipe_tc_be_rate;
1262 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1264 if (s->tc_ov != tc_be_ov) {
1265 RTE_LOG(DEBUG, SCHED,
1266 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1267 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1270 /* Reset the pipe */
1271 memset(p, 0, sizeof(struct rte_sched_pipe));
1277 /* Apply the new pipe configuration */
1278 p->profile = profile;
1279 params = s->pipe_profiles + p->profile;
1281 /* Token Bucket (TB) */
1282 p->tb_time = port->time;
1283 p->tb_credits = params->tb_size / 2;
1285 /* Traffic Classes (TCs) */
1286 p->tc_time = port->time + params->tc_period;
1288 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1290 p->tc_credits[i] = params->tc_credits_per_period[i];
1293 /* Subport best effort tc oversubscription */
1294 double subport_tc_be_rate =
1295 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1296 / (double) s->tc_period;
1297 double pipe_tc_be_rate =
1298 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1299 / (double) params->tc_period;
1300 uint32_t tc_be_ov = s->tc_ov;
1302 s->tc_ov_n += params->tc_ov_weight;
1303 s->tc_ov_rate += pipe_tc_be_rate;
1304 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1306 if (s->tc_ov != tc_be_ov) {
1307 RTE_LOG(DEBUG, SCHED,
1308 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1309 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1311 p->tc_ov_period_id = s->tc_ov_period_id;
1312 p->tc_ov_credits = s->tc_ov_wm;
1319 rte_sched_subport_pipe_profile_add(struct rte_sched_port *port,
1320 uint32_t subport_id,
1321 struct rte_sched_pipe_params *params,
1322 uint32_t *pipe_profile_id)
1324 struct rte_sched_subport *s;
1325 struct rte_sched_pipe_profile *pp;
1332 "%s: Incorrect value for parameter port\n", __func__);
1336 /* Subport id not exceeds the max limit */
1337 if (subport_id > port->n_subports_per_port) {
1339 "%s: Incorrect value for subport id\n", __func__);
1343 s = port->subports[subport_id];
1345 /* Pipe profiles exceeds the max limit */
1346 if (s->n_pipe_profiles >= s->n_max_pipe_profiles) {
1348 "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1353 status = pipe_profile_check(params, port->rate, &s->qsize[0]);
1356 "%s: Pipe profile check failed(%d)\n", __func__, status);
1360 pp = &s->pipe_profiles[s->n_pipe_profiles];
1361 rte_sched_pipe_profile_convert(s, params, pp, port->rate);
1363 /* Pipe profile should not exists */
1364 for (i = 0; i < s->n_pipe_profiles; i++)
1365 if (memcmp(s->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1367 "%s: Pipe profile exists\n", __func__);
1371 /* Pipe profile commit */
1372 *pipe_profile_id = s->n_pipe_profiles;
1373 s->n_pipe_profiles++;
1375 if (s->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1376 s->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1378 rte_sched_port_log_pipe_profile(s, *pipe_profile_id);
1383 static inline uint32_t
1384 rte_sched_port_qindex(struct rte_sched_port *port,
1387 uint32_t traffic_class,
1390 return ((subport & (port->n_subports_per_port - 1)) <<
1391 (port->n_pipes_per_subport_log2 + 4)) |
1393 (port->subports[subport]->n_pipes_per_subport_enabled - 1)) << 4) |
1394 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1395 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1399 rte_sched_port_pkt_write(struct rte_sched_port *port,
1400 struct rte_mbuf *pkt,
1401 uint32_t subport, uint32_t pipe,
1402 uint32_t traffic_class,
1403 uint32_t queue, enum rte_color color)
1406 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1408 rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1412 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1413 const struct rte_mbuf *pkt,
1414 uint32_t *subport, uint32_t *pipe,
1415 uint32_t *traffic_class, uint32_t *queue)
1417 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1419 *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1420 *pipe = (queue_id >> 4) &
1421 (port->subports[*subport]->n_pipes_per_subport_enabled - 1);
1422 *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1423 *queue = rte_sched_port_tc_queue(port, queue_id);
1427 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1429 return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1433 rte_sched_subport_read_stats(struct rte_sched_port *port,
1434 uint32_t subport_id,
1435 struct rte_sched_subport_stats *stats,
1438 struct rte_sched_subport *s;
1440 /* Check user parameters */
1443 "%s: Incorrect value for parameter port\n", __func__);
1447 if (subport_id >= port->n_subports_per_port) {
1449 "%s: Incorrect value for subport id\n", __func__);
1453 if (stats == NULL) {
1455 "%s: Incorrect value for parameter stats\n", __func__);
1459 if (tc_ov == NULL) {
1461 "%s: Incorrect value for tc_ov\n", __func__);
1465 s = port->subports[subport_id];
1467 /* Copy subport stats and clear */
1468 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1469 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1471 /* Subport TC oversubscription status */
1478 rte_sched_queue_read_stats(struct rte_sched_port *port,
1480 struct rte_sched_queue_stats *stats,
1483 struct rte_sched_queue *q;
1484 struct rte_sched_queue_extra *qe;
1486 /* Check user parameters */
1489 "%s: Incorrect value for parameter port\n", __func__);
1493 if (queue_id >= rte_sched_port_queues_per_port(port)) {
1495 "%s: Incorrect value for queue id\n", __func__);
1499 if (stats == NULL) {
1501 "%s: Incorrect value for parameter stats\n", __func__);
1507 "%s: Incorrect value for parameter qlen\n", __func__);
1510 q = port->queue + queue_id;
1511 qe = port->queue_extra + queue_id;
1513 /* Copy queue stats and clear */
1514 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1515 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1518 *qlen = q->qw - q->qr;
1523 #ifdef RTE_SCHED_DEBUG
1526 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1528 struct rte_sched_queue *queue = port->queue + qindex;
1530 return queue->qr == queue->qw;
1533 #endif /* RTE_SCHED_DEBUG */
1535 #ifdef RTE_SCHED_COLLECT_STATS
1538 rte_sched_port_update_subport_stats(struct rte_sched_port *port,
1539 struct rte_sched_subport *subport,
1541 struct rte_mbuf *pkt)
1543 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1544 uint32_t pkt_len = pkt->pkt_len;
1546 subport->stats.n_pkts_tc[tc_index] += 1;
1547 subport->stats.n_bytes_tc[tc_index] += pkt_len;
1550 #ifdef RTE_SCHED_RED
1552 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1553 struct rte_sched_subport *subport,
1555 struct rte_mbuf *pkt,
1559 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1560 struct rte_sched_subport *subport,
1562 struct rte_mbuf *pkt,
1563 __rte_unused uint32_t red)
1566 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1567 uint32_t pkt_len = pkt->pkt_len;
1569 subport->stats.n_pkts_tc_dropped[tc_index] += 1;
1570 subport->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1571 #ifdef RTE_SCHED_RED
1572 subport->stats.n_pkts_red_dropped[tc_index] += red;
1577 rte_sched_port_update_queue_stats(struct rte_sched_subport *subport,
1579 struct rte_mbuf *pkt)
1581 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1582 uint32_t pkt_len = pkt->pkt_len;
1584 qe->stats.n_pkts += 1;
1585 qe->stats.n_bytes += pkt_len;
1588 #ifdef RTE_SCHED_RED
1590 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1592 struct rte_mbuf *pkt,
1596 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1598 struct rte_mbuf *pkt,
1599 __rte_unused uint32_t red)
1602 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1603 uint32_t pkt_len = pkt->pkt_len;
1605 qe->stats.n_pkts_dropped += 1;
1606 qe->stats.n_bytes_dropped += pkt_len;
1607 #ifdef RTE_SCHED_RED
1608 qe->stats.n_pkts_red_dropped += red;
1612 #endif /* RTE_SCHED_COLLECT_STATS */
1614 #ifdef RTE_SCHED_RED
1617 rte_sched_port_red_drop(struct rte_sched_port *port,
1618 struct rte_sched_subport *subport,
1619 struct rte_mbuf *pkt,
1623 struct rte_sched_queue_extra *qe;
1624 struct rte_red_config *red_cfg;
1625 struct rte_red *red;
1627 enum rte_color color;
1629 tc_index = rte_sched_port_pipe_tc(port, qindex);
1630 color = rte_sched_port_pkt_read_color(pkt);
1631 red_cfg = &subport->red_config[tc_index][color];
1633 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1636 qe = subport->queue_extra + qindex;
1639 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1643 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1645 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1646 struct rte_red *red = &qe->red;
1648 rte_red_mark_queue_empty(red, port->time);
1653 static inline int rte_sched_port_red_drop(struct rte_sched_port *port __rte_unused,
1654 struct rte_sched_subport *subport __rte_unused,
1655 struct rte_mbuf *pkt __rte_unused,
1656 uint32_t qindex __rte_unused,
1657 uint16_t qlen __rte_unused)
1662 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1664 #endif /* RTE_SCHED_RED */
1666 #ifdef RTE_SCHED_DEBUG
1669 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1676 rte_panic("Empty slab at position %u\n", bmp_pos);
1679 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1680 if (mask & bmp_slab) {
1681 if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1682 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1689 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1693 #endif /* RTE_SCHED_DEBUG */
1695 static inline struct rte_sched_subport *
1696 rte_sched_port_subport(struct rte_sched_port *port,
1697 struct rte_mbuf *pkt)
1699 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1700 uint32_t subport_id = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1702 return port->subports[subport_id];
1705 static inline uint32_t
1706 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_subport *subport,
1707 struct rte_mbuf *pkt, uint32_t subport_qmask)
1709 struct rte_sched_queue *q;
1710 #ifdef RTE_SCHED_COLLECT_STATS
1711 struct rte_sched_queue_extra *qe;
1713 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1714 uint32_t subport_queue_id = subport_qmask & qindex;
1716 q = subport->queue + subport_queue_id;
1718 #ifdef RTE_SCHED_COLLECT_STATS
1719 qe = subport->queue_extra + subport_queue_id;
1723 return subport_queue_id;
1727 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1728 struct rte_sched_subport *subport,
1730 struct rte_mbuf **qbase)
1732 struct rte_sched_queue *q;
1733 struct rte_mbuf **q_qw;
1736 q = subport->queue + qindex;
1737 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1738 q_qw = qbase + (q->qw & (qsize - 1));
1740 rte_prefetch0(q_qw);
1741 rte_bitmap_prefetch0(subport->bmp, qindex);
1745 rte_sched_port_enqueue_qwa(struct rte_sched_port *port,
1746 struct rte_sched_subport *subport,
1748 struct rte_mbuf **qbase,
1749 struct rte_mbuf *pkt)
1751 struct rte_sched_queue *q;
1755 q = subport->queue + qindex;
1756 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1757 qlen = q->qw - q->qr;
1759 /* Drop the packet (and update drop stats) when queue is full */
1760 if (unlikely(rte_sched_port_red_drop(port, subport, pkt, qindex, qlen) ||
1762 rte_pktmbuf_free(pkt);
1763 #ifdef RTE_SCHED_COLLECT_STATS
1764 rte_sched_port_update_subport_stats_on_drop(port, subport,
1765 qindex, pkt, qlen < qsize);
1766 rte_sched_port_update_queue_stats_on_drop(subport, qindex, pkt,
1772 /* Enqueue packet */
1773 qbase[q->qw & (qsize - 1)] = pkt;
1776 /* Activate queue in the subport bitmap */
1777 rte_bitmap_set(subport->bmp, qindex);
1780 #ifdef RTE_SCHED_COLLECT_STATS
1781 rte_sched_port_update_subport_stats(port, subport, qindex, pkt);
1782 rte_sched_port_update_queue_stats(subport, qindex, pkt);
1790 * The enqueue function implements a 4-level pipeline with each stage
1791 * processing two different packets. The purpose of using a pipeline
1792 * is to hide the latency of prefetching the data structures. The
1793 * naming convention is presented in the diagram below:
1795 * p00 _______ p10 _______ p20 _______ p30 _______
1796 * ----->| |----->| |----->| |----->| |----->
1797 * | 0 | | 1 | | 2 | | 3 |
1798 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1803 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1806 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1807 *pkt30, *pkt31, *pkt_last;
1808 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1809 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1810 struct rte_sched_subport *subport00, *subport01, *subport10, *subport11,
1811 *subport20, *subport21, *subport30, *subport31, *subport_last;
1812 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1813 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1814 uint32_t subport_qmask;
1818 subport_qmask = (1 << (port->n_pipes_per_subport_log2 + 4)) - 1;
1821 * Less then 6 input packets available, which is not enough to
1824 if (unlikely(n_pkts < 6)) {
1825 struct rte_sched_subport *subports[5];
1826 struct rte_mbuf **q_base[5];
1829 /* Prefetch the mbuf structure of each packet */
1830 for (i = 0; i < n_pkts; i++)
1831 rte_prefetch0(pkts[i]);
1833 /* Prefetch the subport structure for each packet */
1834 for (i = 0; i < n_pkts; i++)
1835 subports[i] = rte_sched_port_subport(port, pkts[i]);
1837 /* Prefetch the queue structure for each queue */
1838 for (i = 0; i < n_pkts; i++)
1839 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(subports[i],
1840 pkts[i], subport_qmask);
1842 /* Prefetch the write pointer location of each queue */
1843 for (i = 0; i < n_pkts; i++) {
1844 q_base[i] = rte_sched_subport_pipe_qbase(subports[i], q[i]);
1845 rte_sched_port_enqueue_qwa_prefetch0(port, subports[i],
1849 /* Write each packet to its queue */
1850 for (i = 0; i < n_pkts; i++)
1851 result += rte_sched_port_enqueue_qwa(port, subports[i],
1852 q[i], q_base[i], pkts[i]);
1857 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1860 rte_prefetch0(pkt20);
1861 rte_prefetch0(pkt21);
1865 rte_prefetch0(pkt10);
1866 rte_prefetch0(pkt11);
1868 subport20 = rte_sched_port_subport(port, pkt20);
1869 subport21 = rte_sched_port_subport(port, pkt21);
1870 q20 = rte_sched_port_enqueue_qptrs_prefetch0(subport20,
1871 pkt20, subport_qmask);
1872 q21 = rte_sched_port_enqueue_qptrs_prefetch0(subport21,
1873 pkt21, subport_qmask);
1877 rte_prefetch0(pkt00);
1878 rte_prefetch0(pkt01);
1880 subport10 = rte_sched_port_subport(port, pkt10);
1881 subport11 = rte_sched_port_subport(port, pkt11);
1882 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
1883 pkt10, subport_qmask);
1884 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
1885 pkt11, subport_qmask);
1887 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
1888 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
1889 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
1890 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
1892 /* Run the pipeline */
1893 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1894 /* Propagate stage inputs */
1905 subport30 = subport20;
1906 subport31 = subport21;
1907 subport20 = subport10;
1908 subport21 = subport11;
1909 q30_base = q20_base;
1910 q31_base = q21_base;
1912 /* Stage 0: Get packets in */
1914 pkt01 = pkts[i + 1];
1915 rte_prefetch0(pkt00);
1916 rte_prefetch0(pkt01);
1918 /* Stage 1: Prefetch subport and queue structure storing queue pointers */
1919 subport10 = rte_sched_port_subport(port, pkt10);
1920 subport11 = rte_sched_port_subport(port, pkt11);
1921 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
1922 pkt10, subport_qmask);
1923 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
1924 pkt11, subport_qmask);
1926 /* Stage 2: Prefetch queue write location */
1927 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
1928 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
1929 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
1930 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
1932 /* Stage 3: Write packet to queue and activate queue */
1933 r30 = rte_sched_port_enqueue_qwa(port, subport30,
1934 q30, q30_base, pkt30);
1935 r31 = rte_sched_port_enqueue_qwa(port, subport31,
1936 q31, q31_base, pkt31);
1937 result += r30 + r31;
1941 * Drain the pipeline (exactly 6 packets).
1942 * Handle the last packet in the case
1943 * of an odd number of input packets.
1945 pkt_last = pkts[n_pkts - 1];
1946 rte_prefetch0(pkt_last);
1948 subport00 = rte_sched_port_subport(port, pkt00);
1949 subport01 = rte_sched_port_subport(port, pkt01);
1950 q00 = rte_sched_port_enqueue_qptrs_prefetch0(subport00,
1951 pkt00, subport_qmask);
1952 q01 = rte_sched_port_enqueue_qptrs_prefetch0(subport01,
1953 pkt01, subport_qmask);
1955 q10_base = rte_sched_subport_pipe_qbase(subport10, q10);
1956 q11_base = rte_sched_subport_pipe_qbase(subport11, q11);
1957 rte_sched_port_enqueue_qwa_prefetch0(port, subport10, q10, q10_base);
1958 rte_sched_port_enqueue_qwa_prefetch0(port, subport11, q11, q11_base);
1960 r20 = rte_sched_port_enqueue_qwa(port, subport20,
1961 q20, q20_base, pkt20);
1962 r21 = rte_sched_port_enqueue_qwa(port, subport21,
1963 q21, q21_base, pkt21);
1964 result += r20 + r21;
1966 subport_last = rte_sched_port_subport(port, pkt_last);
1967 q_last = rte_sched_port_enqueue_qptrs_prefetch0(subport_last,
1968 pkt_last, subport_qmask);
1970 q00_base = rte_sched_subport_pipe_qbase(subport00, q00);
1971 q01_base = rte_sched_subport_pipe_qbase(subport01, q01);
1972 rte_sched_port_enqueue_qwa_prefetch0(port, subport00, q00, q00_base);
1973 rte_sched_port_enqueue_qwa_prefetch0(port, subport01, q01, q01_base);
1975 r10 = rte_sched_port_enqueue_qwa(port, subport10, q10,
1977 r11 = rte_sched_port_enqueue_qwa(port, subport11, q11,
1979 result += r10 + r11;
1981 q_last_base = rte_sched_subport_pipe_qbase(subport_last, q_last);
1982 rte_sched_port_enqueue_qwa_prefetch0(port, subport_last,
1983 q_last, q_last_base);
1985 r00 = rte_sched_port_enqueue_qwa(port, subport00, q00,
1987 r01 = rte_sched_port_enqueue_qwa(port, subport01, q01,
1989 result += r00 + r01;
1992 r_last = rte_sched_port_enqueue_qwa(port, subport_last,
1993 q_last, q_last_base, pkt_last);
2000 #ifndef RTE_SCHED_SUBPORT_TC_OV
2003 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
2005 struct rte_sched_grinder *grinder = port->grinder + pos;
2006 struct rte_sched_subport *subport = grinder->subport;
2007 struct rte_sched_pipe *pipe = grinder->pipe;
2008 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2013 n_periods = (port->time - subport->tb_time) / subport->tb_period;
2014 subport->tb_credits += n_periods * subport->tb_credits_per_period;
2015 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
2016 subport->tb_time += n_periods * subport->tb_period;
2019 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2020 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2021 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
2022 pipe->tb_time += n_periods * params->tb_period;
2025 if (unlikely(port->time >= subport->tc_time)) {
2026 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2027 subport->tc_credits[i] = subport->tc_credits_per_period[i];
2029 subport->tc_time = port->time + subport->tc_period;
2033 if (unlikely(port->time >= pipe->tc_time)) {
2034 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2035 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2037 pipe->tc_time = port->time + params->tc_period;
2043 static inline uint32_t
2044 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
2046 struct rte_sched_grinder *grinder = port->grinder + pos;
2047 struct rte_sched_subport *subport = grinder->subport;
2048 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2049 uint32_t tc_consumption = 0, tc_ov_consumption_max;
2050 uint32_t tc_ov_wm = subport->tc_ov_wm;
2053 if (subport->tc_ov == 0)
2054 return subport->tc_ov_wm_max;
2056 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2057 tc_ov_consumption[i] =
2058 subport->tc_credits_per_period[i] - subport->tc_credits[i];
2059 tc_consumption += tc_ov_consumption[i];
2062 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
2063 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2064 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2066 tc_ov_consumption_max =
2067 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2070 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2071 (tc_ov_consumption_max - port->mtu)) {
2072 tc_ov_wm -= tc_ov_wm >> 7;
2073 if (tc_ov_wm < subport->tc_ov_wm_min)
2074 tc_ov_wm = subport->tc_ov_wm_min;
2079 tc_ov_wm += (tc_ov_wm >> 7) + 1;
2080 if (tc_ov_wm > subport->tc_ov_wm_max)
2081 tc_ov_wm = subport->tc_ov_wm_max;
2087 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
2089 struct rte_sched_grinder *grinder = port->grinder + pos;
2090 struct rte_sched_subport *subport = grinder->subport;
2091 struct rte_sched_pipe *pipe = grinder->pipe;
2092 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2097 n_periods = (port->time - subport->tb_time) / subport->tb_period;
2098 subport->tb_credits += n_periods * subport->tb_credits_per_period;
2099 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
2100 subport->tb_time += n_periods * subport->tb_period;
2103 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2104 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2105 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
2106 pipe->tb_time += n_periods * params->tb_period;
2109 if (unlikely(port->time >= subport->tc_time)) {
2110 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
2112 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2113 subport->tc_credits[i] = subport->tc_credits_per_period[i];
2115 subport->tc_time = port->time + subport->tc_period;
2116 subport->tc_ov_period_id++;
2120 if (unlikely(port->time >= pipe->tc_time)) {
2121 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2122 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2123 pipe->tc_time = port->time + params->tc_period;
2126 /* Pipe TCs - Oversubscription */
2127 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2128 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2130 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2134 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
2137 #ifndef RTE_SCHED_SUBPORT_TC_OV
2140 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
2142 struct rte_sched_grinder *grinder = port->grinder + pos;
2143 struct rte_sched_subport *subport = grinder->subport;
2144 struct rte_sched_pipe *pipe = grinder->pipe;
2145 struct rte_mbuf *pkt = grinder->pkt;
2146 uint32_t tc_index = grinder->tc_index;
2147 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2148 uint32_t subport_tb_credits = subport->tb_credits;
2149 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2150 uint32_t pipe_tb_credits = pipe->tb_credits;
2151 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2154 /* Check queue credits */
2155 enough_credits = (pkt_len <= subport_tb_credits) &&
2156 (pkt_len <= subport_tc_credits) &&
2157 (pkt_len <= pipe_tb_credits) &&
2158 (pkt_len <= pipe_tc_credits);
2160 if (!enough_credits)
2163 /* Update port credits */
2164 subport->tb_credits -= pkt_len;
2165 subport->tc_credits[tc_index] -= pkt_len;
2166 pipe->tb_credits -= pkt_len;
2167 pipe->tc_credits[tc_index] -= pkt_len;
2175 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
2177 struct rte_sched_grinder *grinder = port->grinder + pos;
2178 struct rte_sched_subport *subport = grinder->subport;
2179 struct rte_sched_pipe *pipe = grinder->pipe;
2180 struct rte_mbuf *pkt = grinder->pkt;
2181 uint32_t tc_index = grinder->tc_index;
2182 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2183 uint32_t subport_tb_credits = subport->tb_credits;
2184 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2185 uint32_t pipe_tb_credits = pipe->tb_credits;
2186 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2187 uint32_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2188 uint32_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2189 uint32_t pipe_tc_ov_credits, i;
2192 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2193 pipe_tc_ov_mask1[i] = UINT32_MAX;
2195 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2196 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = UINT32_MAX;
2197 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2199 /* Check pipe and subport credits */
2200 enough_credits = (pkt_len <= subport_tb_credits) &&
2201 (pkt_len <= subport_tc_credits) &&
2202 (pkt_len <= pipe_tb_credits) &&
2203 (pkt_len <= pipe_tc_credits) &&
2204 (pkt_len <= pipe_tc_ov_credits);
2206 if (!enough_credits)
2209 /* Update pipe and subport credits */
2210 subport->tb_credits -= pkt_len;
2211 subport->tc_credits[tc_index] -= pkt_len;
2212 pipe->tb_credits -= pkt_len;
2213 pipe->tc_credits[tc_index] -= pkt_len;
2214 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2219 #endif /* RTE_SCHED_SUBPORT_TC_OV */
2223 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
2225 struct rte_sched_grinder *grinder = port->grinder + pos;
2226 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2227 struct rte_mbuf *pkt = grinder->pkt;
2228 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2229 uint32_t be_tc_active;
2231 if (!grinder_credits_check(port, pos))
2234 /* Advance port time */
2235 port->time += pkt_len;
2238 port->pkts_out[port->n_pkts_out++] = pkt;
2241 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2242 grinder->wrr_tokens[grinder->qpos] +=
2243 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2245 if (queue->qr == queue->qw) {
2246 uint32_t qindex = grinder->qindex[grinder->qpos];
2248 rte_bitmap_clear(port->bmp, qindex);
2249 grinder->qmask &= ~(1 << grinder->qpos);
2251 grinder->wrr_mask[grinder->qpos] = 0;
2252 rte_sched_port_set_queue_empty_timestamp(port, qindex);
2255 /* Reset pipe loop detection */
2256 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2257 grinder->productive = 1;
2262 #ifdef SCHED_VECTOR_SSE4
2265 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2267 __m128i index = _mm_set1_epi32(base_pipe);
2268 __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
2269 __m128i res = _mm_cmpeq_epi32(pipes, index);
2271 pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
2272 pipes = _mm_cmpeq_epi32(pipes, index);
2273 res = _mm_or_si128(res, pipes);
2275 if (_mm_testz_si128(res, res))
2281 #elif defined(SCHED_VECTOR_NEON)
2284 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2286 uint32x4_t index, pipes;
2287 uint32_t *pos = (uint32_t *)port->grinder_base_bmp_pos;
2289 index = vmovq_n_u32(base_pipe);
2290 pipes = vld1q_u32(pos);
2291 if (!vminvq_u32(veorq_u32(pipes, index)))
2294 pipes = vld1q_u32(pos + 4);
2295 if (!vminvq_u32(veorq_u32(pipes, index)))
2304 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2308 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2309 if (port->grinder_base_bmp_pos[i] == base_pipe)
2316 #endif /* RTE_SCHED_OPTIMIZATIONS */
2319 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2321 struct rte_sched_grinder *grinder = port->grinder + pos;
2324 grinder->pcache_w = 0;
2325 grinder->pcache_r = 0;
2327 w[0] = (uint16_t) bmp_slab;
2328 w[1] = (uint16_t) (bmp_slab >> 16);
2329 w[2] = (uint16_t) (bmp_slab >> 32);
2330 w[3] = (uint16_t) (bmp_slab >> 48);
2332 grinder->pcache_qmask[grinder->pcache_w] = w[0];
2333 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2334 grinder->pcache_w += (w[0] != 0);
2336 grinder->pcache_qmask[grinder->pcache_w] = w[1];
2337 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2338 grinder->pcache_w += (w[1] != 0);
2340 grinder->pcache_qmask[grinder->pcache_w] = w[2];
2341 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2342 grinder->pcache_w += (w[2] != 0);
2344 grinder->pcache_qmask[grinder->pcache_w] = w[3];
2345 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2346 grinder->pcache_w += (w[3] != 0);
2350 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
2352 struct rte_sched_grinder *grinder = port->grinder + pos;
2355 grinder->tccache_w = 0;
2356 grinder->tccache_r = 0;
2358 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2359 b = (uint8_t) ((qmask >> i) & 0x1);
2360 grinder->tccache_qmask[grinder->tccache_w] = b;
2361 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2362 grinder->tccache_w += (b != 0);
2365 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2366 grinder->tccache_qmask[grinder->tccache_w] = b;
2367 grinder->tccache_qindex[grinder->tccache_w] = qindex +
2368 RTE_SCHED_TRAFFIC_CLASS_BE;
2369 grinder->tccache_w += (b != 0);
2373 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
2375 struct rte_sched_grinder *grinder = port->grinder + pos;
2376 struct rte_mbuf **qbase;
2380 if (grinder->tccache_r == grinder->tccache_w)
2383 qindex = grinder->tccache_qindex[grinder->tccache_r];
2384 qbase = rte_sched_port_qbase(port, qindex);
2385 qsize = rte_sched_port_qsize(port, qindex);
2387 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2388 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2389 grinder->qsize = qsize;
2391 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2392 grinder->queue[0] = port->queue + qindex;
2393 grinder->qbase[0] = qbase;
2394 grinder->qindex[0] = qindex;
2395 grinder->tccache_r++;
2400 grinder->queue[0] = port->queue + qindex;
2401 grinder->queue[1] = port->queue + qindex + 1;
2402 grinder->queue[2] = port->queue + qindex + 2;
2403 grinder->queue[3] = port->queue + qindex + 3;
2405 grinder->qbase[0] = qbase;
2406 grinder->qbase[1] = qbase + qsize;
2407 grinder->qbase[2] = qbase + 2 * qsize;
2408 grinder->qbase[3] = qbase + 3 * qsize;
2410 grinder->qindex[0] = qindex;
2411 grinder->qindex[1] = qindex + 1;
2412 grinder->qindex[2] = qindex + 2;
2413 grinder->qindex[3] = qindex + 3;
2415 grinder->tccache_r++;
2420 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
2422 struct rte_sched_grinder *grinder = port->grinder + pos;
2423 uint32_t pipe_qindex;
2424 uint16_t pipe_qmask;
2426 if (grinder->pcache_r < grinder->pcache_w) {
2427 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2428 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2429 grinder->pcache_r++;
2431 uint64_t bmp_slab = 0;
2432 uint32_t bmp_pos = 0;
2434 /* Get another non-empty pipe group */
2435 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
2438 #ifdef RTE_SCHED_DEBUG
2439 debug_check_queue_slab(port, bmp_pos, bmp_slab);
2442 /* Return if pipe group already in one of the other grinders */
2443 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2444 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
2447 port->grinder_base_bmp_pos[pos] = bmp_pos;
2449 /* Install new pipe group into grinder's pipe cache */
2450 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
2452 pipe_qmask = grinder->pcache_qmask[0];
2453 pipe_qindex = grinder->pcache_qindex[0];
2454 grinder->pcache_r = 1;
2457 /* Install new pipe in the grinder */
2458 grinder->pindex = pipe_qindex >> 4;
2459 grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
2460 grinder->pipe = port->pipe + grinder->pindex;
2461 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2462 grinder->productive = 0;
2464 grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
2465 grinder_next_tc(port, pos);
2467 /* Check for pipe exhaustion */
2468 if (grinder->pindex == port->pipe_loop) {
2469 port->pipe_exhaustion = 1;
2470 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2478 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
2480 struct rte_sched_grinder *grinder = port->grinder + pos;
2481 struct rte_sched_pipe *pipe = grinder->pipe;
2482 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2483 uint32_t qmask = grinder->qmask;
2485 grinder->wrr_tokens[0] =
2486 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2487 grinder->wrr_tokens[1] =
2488 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2489 grinder->wrr_tokens[2] =
2490 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2491 grinder->wrr_tokens[3] =
2492 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2494 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2495 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2496 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2497 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2499 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2500 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2501 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2502 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2506 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
2508 struct rte_sched_grinder *grinder = port->grinder + pos;
2509 struct rte_sched_pipe *pipe = grinder->pipe;
2511 pipe->wrr_tokens[0] =
2512 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2513 RTE_SCHED_WRR_SHIFT;
2514 pipe->wrr_tokens[1] =
2515 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2516 RTE_SCHED_WRR_SHIFT;
2517 pipe->wrr_tokens[2] =
2518 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2519 RTE_SCHED_WRR_SHIFT;
2520 pipe->wrr_tokens[3] =
2521 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2522 RTE_SCHED_WRR_SHIFT;
2526 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
2528 struct rte_sched_grinder *grinder = port->grinder + pos;
2529 uint16_t wrr_tokens_min;
2531 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2532 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2533 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2534 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2536 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2537 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2539 grinder->wrr_tokens[0] -= wrr_tokens_min;
2540 grinder->wrr_tokens[1] -= wrr_tokens_min;
2541 grinder->wrr_tokens[2] -= wrr_tokens_min;
2542 grinder->wrr_tokens[3] -= wrr_tokens_min;
2546 #define grinder_evict(port, pos)
2549 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
2551 struct rte_sched_grinder *grinder = port->grinder + pos;
2553 rte_prefetch0(grinder->pipe);
2554 rte_prefetch0(grinder->queue[0]);
2558 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
2560 struct rte_sched_grinder *grinder = port->grinder + pos;
2561 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2563 qsize = grinder->qsize;
2566 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2567 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2569 rte_prefetch0(grinder->qbase[0] + qr[0]);
2573 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2574 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2575 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2576 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2578 rte_prefetch0(grinder->qbase[0] + qr[0]);
2579 rte_prefetch0(grinder->qbase[1] + qr[1]);
2581 grinder_wrr_load(port, pos);
2582 grinder_wrr(port, pos);
2584 rte_prefetch0(grinder->qbase[2] + qr[2]);
2585 rte_prefetch0(grinder->qbase[3] + qr[3]);
2589 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2591 struct rte_sched_grinder *grinder = port->grinder + pos;
2592 uint32_t qpos = grinder->qpos;
2593 struct rte_mbuf **qbase = grinder->qbase[qpos];
2594 uint16_t qsize = grinder->qsize;
2595 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2597 grinder->pkt = qbase[qr];
2598 rte_prefetch0(grinder->pkt);
2600 if (unlikely((qr & 0x7) == 7)) {
2601 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2603 rte_prefetch0(qbase + qr_next);
2607 static inline uint32_t
2608 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2610 struct rte_sched_grinder *grinder = port->grinder + pos;
2612 switch (grinder->state) {
2613 case e_GRINDER_PREFETCH_PIPE:
2615 if (grinder_next_pipe(port, pos)) {
2616 grinder_prefetch_pipe(port, pos);
2617 port->busy_grinders++;
2619 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2626 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2628 struct rte_sched_pipe *pipe = grinder->pipe;
2630 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2631 grinder_prefetch_tc_queue_arrays(port, pos);
2632 grinder_credits_update(port, pos);
2634 grinder->state = e_GRINDER_PREFETCH_MBUF;
2638 case e_GRINDER_PREFETCH_MBUF:
2640 grinder_prefetch_mbuf(port, pos);
2642 grinder->state = e_GRINDER_READ_MBUF;
2646 case e_GRINDER_READ_MBUF:
2648 uint32_t wrr_active, result = 0;
2650 result = grinder_schedule(port, pos);
2652 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2654 /* Look for next packet within the same TC */
2655 if (result && grinder->qmask) {
2657 grinder_wrr(port, pos);
2659 grinder_prefetch_mbuf(port, pos);
2665 grinder_wrr_store(port, pos);
2667 /* Look for another active TC within same pipe */
2668 if (grinder_next_tc(port, pos)) {
2669 grinder_prefetch_tc_queue_arrays(port, pos);
2671 grinder->state = e_GRINDER_PREFETCH_MBUF;
2675 if (grinder->productive == 0 &&
2676 port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2677 port->pipe_loop = grinder->pindex;
2679 grinder_evict(port, pos);
2681 /* Look for another active pipe */
2682 if (grinder_next_pipe(port, pos)) {
2683 grinder_prefetch_pipe(port, pos);
2685 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2689 /* No active pipe found */
2690 port->busy_grinders--;
2692 grinder->state = e_GRINDER_PREFETCH_PIPE;
2697 rte_panic("Algorithmic error (invalid state)\n");
2703 rte_sched_port_time_resync(struct rte_sched_port *port)
2705 uint64_t cycles = rte_get_tsc_cycles();
2706 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2707 uint64_t bytes_diff;
2709 /* Compute elapsed time in bytes */
2710 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2711 port->inv_cycles_per_byte);
2713 /* Advance port time */
2714 port->time_cpu_cycles = cycles;
2715 port->time_cpu_bytes += bytes_diff;
2716 if (port->time < port->time_cpu_bytes)
2717 port->time = port->time_cpu_bytes;
2719 /* Reset pipe loop detection */
2720 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2724 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2728 /* Check if any exception flag is set */
2729 exceptions = (second_pass && port->busy_grinders == 0) ||
2730 (port->pipe_exhaustion == 1);
2732 /* Clear exception flags */
2733 port->pipe_exhaustion = 0;
2739 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2743 port->pkts_out = pkts;
2744 port->n_pkts_out = 0;
2746 rte_sched_port_time_resync(port);
2748 /* Take each queue in the grinder one step further */
2749 for (i = 0, count = 0; ; i++) {
2750 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2751 if ((count == n_pkts) ||
2752 rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {