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;
221 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cyles */
222 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
223 uint64_t time; /* Current NIC TX time measured in bytes */
224 struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
227 struct rte_mbuf **pkts_out;
231 /* Large data structures */
232 struct rte_sched_subport *subports[0] __rte_cache_aligned;
233 } __rte_cache_aligned;
235 enum rte_sched_subport_array {
236 e_RTE_SCHED_SUBPORT_ARRAY_PIPE = 0,
237 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE,
238 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA,
239 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES,
240 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY,
241 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY,
242 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL,
245 static inline uint32_t
246 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
248 return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
251 static inline struct rte_mbuf **
252 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
254 uint32_t pindex = qindex >> 4;
255 uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
257 return (subport->queue_array + pindex *
258 subport->qsize_sum + subport->qsize_add[qpos]);
261 static inline uint16_t
262 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
263 struct rte_sched_subport *subport, uint32_t qindex)
265 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
267 return subport->qsize[tc];
270 static inline uint32_t
271 rte_sched_port_queues_per_port(struct rte_sched_port *port)
273 uint32_t n_queues = 0, i;
275 for (i = 0; i < port->n_subports_per_port; i++)
276 n_queues += rte_sched_subport_pipe_queues(port->subports[i]);
281 static inline uint16_t
282 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
284 uint16_t pipe_queue = port->pipe_queue[traffic_class];
289 static inline uint8_t
290 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
292 uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
297 static inline uint8_t
298 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
300 uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
306 pipe_profile_check(struct rte_sched_pipe_params *params,
307 uint32_t rate, uint16_t *qsize)
311 /* Pipe parameters */
312 if (params == NULL) {
314 "%s: Incorrect value for parameter params\n", __func__);
318 /* TB rate: non-zero, not greater than port rate */
319 if (params->tb_rate == 0 ||
320 params->tb_rate > rate) {
322 "%s: Incorrect value for tb rate\n", __func__);
326 /* TB size: non-zero */
327 if (params->tb_size == 0) {
329 "%s: Incorrect value for tb size\n", __func__);
333 /* TC rate: non-zero if qsize non-zero, less than pipe rate */
334 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
335 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
336 (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
337 params->tc_rate[i] > params->tb_rate))) {
339 "%s: Incorrect value for qsize or tc_rate\n", __func__);
344 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
345 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
347 "%s: Incorrect value for be traffic class rate\n", __func__);
351 /* TC period: non-zero */
352 if (params->tc_period == 0) {
354 "%s: Incorrect value for tc period\n", __func__);
358 /* Best effort tc oversubscription weight: non-zero */
359 if (params->tc_ov_weight == 0) {
361 "%s: Incorrect value for tc ov weight\n", __func__);
365 /* Queue WRR weights: non-zero */
366 for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
367 if (params->wrr_weights[i] == 0) {
369 "%s: Incorrect value for wrr weight\n", __func__);
378 rte_sched_port_check_params(struct rte_sched_port_params *params)
380 if (params == NULL) {
382 "%s: Incorrect value for parameter params\n", __func__);
387 if (params->socket < 0) {
389 "%s: Incorrect value for socket id\n", __func__);
394 if (params->rate == 0) {
396 "%s: Incorrect value for rate\n", __func__);
401 if (params->mtu == 0) {
403 "%s: Incorrect value for mtu\n", __func__);
407 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
408 if (params->n_subports_per_port == 0 ||
409 params->n_subports_per_port > 1u << 16 ||
410 !rte_is_power_of_2(params->n_subports_per_port)) {
412 "%s: Incorrect value for number of subports\n", __func__);
416 /* n_pipes_per_subport: non-zero, power of 2 */
417 if (params->n_pipes_per_subport == 0 ||
418 !rte_is_power_of_2(params->n_pipes_per_subport)) {
420 "%s: Incorrect value for maximum pipes number\n", __func__);
428 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
429 enum rte_sched_subport_array array)
431 uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
432 uint32_t n_subport_pipe_queues =
433 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
435 uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
436 uint32_t size_queue =
437 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
438 uint32_t size_queue_extra
439 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
440 uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
441 sizeof(struct rte_sched_pipe_profile);
442 uint32_t size_bmp_array =
443 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
444 uint32_t size_per_pipe_queue_array, size_queue_array;
448 size_per_pipe_queue_array = 0;
449 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
450 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
451 size_per_pipe_queue_array +=
452 params->qsize[i] * sizeof(struct rte_mbuf *);
454 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
455 params->qsize[i] * sizeof(struct rte_mbuf *);
457 size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
461 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
463 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
465 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
467 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
469 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
471 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
473 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
475 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
477 if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
479 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
481 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
483 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
489 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
493 subport->qsize_add[0] = 0;
495 /* Strict prority traffic class */
496 for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
497 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
499 /* Best-effort traffic class */
500 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
501 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
502 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
503 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
504 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
505 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
506 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
507 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
508 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
510 subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
511 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
515 rte_sched_port_log_pipe_profile(struct rte_sched_subport *subport, uint32_t i)
517 struct rte_sched_pipe_profile *p = subport->pipe_profiles + i;
519 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
520 " Token bucket: period = %u, credits per period = %u, size = %u\n"
521 " Traffic classes: period = %u,\n"
522 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
523 " Best-effort traffic class oversubscription: weight = %hhu\n"
524 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
529 p->tb_credits_per_period,
532 /* Traffic classes */
534 p->tc_credits_per_period[0],
535 p->tc_credits_per_period[1],
536 p->tc_credits_per_period[2],
537 p->tc_credits_per_period[3],
538 p->tc_credits_per_period[4],
539 p->tc_credits_per_period[5],
540 p->tc_credits_per_period[6],
541 p->tc_credits_per_period[7],
542 p->tc_credits_per_period[8],
543 p->tc_credits_per_period[9],
544 p->tc_credits_per_period[10],
545 p->tc_credits_per_period[11],
546 p->tc_credits_per_period[12],
548 /* Best-effort traffic class oversubscription */
552 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
555 static inline uint64_t
556 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
558 uint64_t time = time_ms;
560 time = (time * rate) / 1000;
566 rte_sched_pipe_profile_convert(struct rte_sched_subport *subport,
567 struct rte_sched_pipe_params *src,
568 struct rte_sched_pipe_profile *dst,
571 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
572 uint32_t lcd1, lcd2, lcd;
576 if (src->tb_rate == rate) {
577 dst->tb_credits_per_period = 1;
580 double tb_rate = (double) src->tb_rate
582 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
584 rte_approx(tb_rate, d,
585 &dst->tb_credits_per_period, &dst->tb_period);
588 dst->tb_size = src->tb_size;
590 /* Traffic Classes */
591 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
594 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
595 if (subport->qsize[i])
596 dst->tc_credits_per_period[i]
597 = rte_sched_time_ms_to_bytes(src->tc_period,
600 dst->tc_ov_weight = src->tc_ov_weight;
603 wrr_cost[0] = src->wrr_weights[0];
604 wrr_cost[1] = src->wrr_weights[1];
605 wrr_cost[2] = src->wrr_weights[2];
606 wrr_cost[3] = src->wrr_weights[3];
608 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
609 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
610 lcd = rte_get_lcd(lcd1, lcd2);
612 wrr_cost[0] = lcd / wrr_cost[0];
613 wrr_cost[1] = lcd / wrr_cost[1];
614 wrr_cost[2] = lcd / wrr_cost[2];
615 wrr_cost[3] = lcd / wrr_cost[3];
617 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
618 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
619 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
620 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
624 rte_sched_subport_config_pipe_profile_table(struct rte_sched_subport *subport,
625 struct rte_sched_subport_params *params, uint32_t rate)
629 for (i = 0; i < subport->n_pipe_profiles; i++) {
630 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
631 struct rte_sched_pipe_profile *dst = subport->pipe_profiles + i;
633 rte_sched_pipe_profile_convert(subport, src, dst, rate);
634 rte_sched_port_log_pipe_profile(subport, i);
637 subport->pipe_tc_be_rate_max = 0;
638 for (i = 0; i < subport->n_pipe_profiles; i++) {
639 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
640 uint32_t pipe_tc_be_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
642 if (subport->pipe_tc_be_rate_max < pipe_tc_be_rate)
643 subport->pipe_tc_be_rate_max = pipe_tc_be_rate;
648 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
649 uint32_t n_max_pipes_per_subport,
654 /* Check user parameters */
655 if (params == NULL) {
657 "%s: Incorrect value for parameter params\n", __func__);
661 if (params->tb_rate == 0 || params->tb_rate > rate) {
663 "%s: Incorrect value for tb rate\n", __func__);
667 if (params->tb_size == 0) {
669 "%s: Incorrect value for tb size\n", __func__);
673 /* qsize: if non-zero, power of 2,
674 * no bigger than 32K (due to 16-bit read/write pointers)
676 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
677 uint16_t qsize = params->qsize[i];
679 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
681 "%s: Incorrect value for qsize\n", __func__);
686 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
687 uint32_t tc_rate = params->tc_rate[i];
688 uint16_t qsize = params->qsize[i];
690 if ((qsize == 0 && tc_rate != 0) ||
691 (qsize != 0 && tc_rate == 0) ||
692 (tc_rate > params->tb_rate)) {
694 "%s: Incorrect value for tc rate\n", __func__);
699 if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
700 params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
702 "%s: Incorrect qsize or tc rate(best effort)\n", __func__);
706 if (params->tc_period == 0) {
708 "%s: Incorrect value for tc period\n", __func__);
712 /* n_pipes_per_subport: non-zero, power of 2 */
713 if (params->n_pipes_per_subport_enabled == 0 ||
714 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
715 !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
717 "%s: Incorrect value for pipes number\n", __func__);
721 /* pipe_profiles and n_pipe_profiles */
722 if (params->pipe_profiles == NULL ||
723 params->n_pipe_profiles == 0 ||
724 params->n_max_pipe_profiles == 0 ||
725 params->n_pipe_profiles > params->n_max_pipe_profiles) {
727 "%s: Incorrect value for pipe profiles\n", __func__);
731 for (i = 0; i < params->n_pipe_profiles; i++) {
732 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
735 status = pipe_profile_check(p, rate, ¶ms->qsize[0]);
738 "%s: Pipe profile check failed(%d)\n", __func__, status);
747 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *port_params,
748 struct rte_sched_subport_params **subport_params)
750 uint32_t size0 = 0, size1 = 0, i;
753 status = rte_sched_port_check_params(port_params);
756 "%s: Port scheduler port params check failed (%d)\n",
762 for (i = 0; i < port_params->n_subports_per_port; i++) {
763 struct rte_sched_subport_params *sp = subport_params[i];
765 status = rte_sched_subport_check_params(sp,
766 port_params->n_pipes_per_subport,
770 "%s: Port scheduler subport params check failed (%d)\n",
777 size0 = sizeof(struct rte_sched_port);
779 for (i = 0; i < port_params->n_subports_per_port; i++) {
780 struct rte_sched_subport_params *sp = subport_params[i];
782 size1 += rte_sched_subport_get_array_base(sp,
783 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
786 return size0 + size1;
789 struct rte_sched_port *
790 rte_sched_port_config(struct rte_sched_port_params *params)
792 struct rte_sched_port *port = NULL;
793 uint32_t size0, size1;
794 uint32_t cycles_per_byte;
798 status = rte_sched_port_check_params(params);
801 "%s: Port scheduler params check failed (%d)\n",
806 size0 = sizeof(struct rte_sched_port);
807 size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
809 /* Allocate memory to store the data structures */
810 port = rte_zmalloc_socket("qos_params", size0 + size1, RTE_CACHE_LINE_SIZE,
813 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
818 /* User parameters */
819 port->n_subports_per_port = params->n_subports_per_port;
820 port->n_pipes_per_subport = params->n_pipes_per_subport;
821 port->n_pipes_per_subport_log2 =
822 __builtin_ctz(params->n_pipes_per_subport);
823 port->socket = params->socket;
825 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
826 port->pipe_queue[i] = i;
828 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
829 port->pipe_tc[i] = j;
831 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
835 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
836 port->tc_queue[i] = j;
838 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
841 port->rate = params->rate;
842 port->mtu = params->mtu + params->frame_overhead;
843 port->frame_overhead = params->frame_overhead;
846 port->time_cpu_cycles = rte_get_tsc_cycles();
847 port->time_cpu_bytes = 0;
850 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
852 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
855 port->pkts_out = NULL;
856 port->n_pkts_out = 0;
857 port->subport_id = 0;
863 rte_sched_subport_free(struct rte_sched_port *port,
864 struct rte_sched_subport *subport)
866 uint32_t n_subport_pipe_queues;
872 n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
874 /* Free enqueued mbufs */
875 for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
876 struct rte_mbuf **mbufs =
877 rte_sched_subport_pipe_qbase(subport, qindex);
878 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
880 struct rte_sched_queue *queue = subport->queue + qindex;
881 uint16_t qr = queue->qr & (qsize - 1);
882 uint16_t qw = queue->qw & (qsize - 1);
884 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
885 rte_pktmbuf_free(mbufs[qr]);
889 rte_bitmap_free(subport->bmp);
893 rte_sched_port_free(struct rte_sched_port *port)
897 /* Check user parameters */
901 for (i = 0; i < port->n_subports_per_port; i++)
902 rte_sched_subport_free(port, port->subports[i]);
908 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
910 struct rte_sched_subport *s = port->subports[i];
912 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
913 " Token bucket: period = %u, credits per period = %u, size = %u\n"
914 " Traffic classes: period = %u\n"
915 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
916 " Best effort traffic class oversubscription: wm min = %u, wm max = %u\n",
921 s->tb_credits_per_period,
924 /* Traffic classes */
926 s->tc_credits_per_period[0],
927 s->tc_credits_per_period[1],
928 s->tc_credits_per_period[2],
929 s->tc_credits_per_period[3],
930 s->tc_credits_per_period[4],
931 s->tc_credits_per_period[5],
932 s->tc_credits_per_period[6],
933 s->tc_credits_per_period[7],
934 s->tc_credits_per_period[8],
935 s->tc_credits_per_period[9],
936 s->tc_credits_per_period[10],
937 s->tc_credits_per_period[11],
938 s->tc_credits_per_period[12],
940 /* Best effort traffic class oversubscription */
946 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
950 for (i = 0; i < n_subports; i++) {
951 struct rte_sched_subport *subport = port->subports[i];
953 rte_sched_subport_free(port, subport);
960 rte_sched_subport_config(struct rte_sched_port *port,
962 struct rte_sched_subport_params *params)
964 struct rte_sched_subport *s = NULL;
965 uint32_t n_subports = subport_id;
966 uint32_t n_subport_pipe_queues, i;
967 uint32_t size0, size1, bmp_mem_size;
970 /* Check user parameters */
973 "%s: Incorrect value for parameter port\n", __func__);
977 if (subport_id >= port->n_subports_per_port) {
979 "%s: Incorrect value for subport id\n", __func__);
981 rte_sched_free_memory(port, n_subports);
985 status = rte_sched_subport_check_params(params,
986 port->n_pipes_per_subport,
989 RTE_LOG(NOTICE, SCHED,
990 "%s: Port scheduler params check failed (%d)\n",
993 rte_sched_free_memory(port, n_subports);
997 /* Determine the amount of memory to allocate */
998 size0 = sizeof(struct rte_sched_subport);
999 size1 = rte_sched_subport_get_array_base(params,
1000 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1002 /* Allocate memory to store the data structures */
1003 s = rte_zmalloc_socket("subport_params", size0 + size1,
1004 RTE_CACHE_LINE_SIZE, port->socket);
1007 "%s: Memory allocation fails\n", __func__);
1009 rte_sched_free_memory(port, n_subports);
1016 port->subports[subport_id] = s;
1018 /* Token Bucket (TB) */
1019 if (params->tb_rate == port->rate) {
1020 s->tb_credits_per_period = 1;
1023 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
1024 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
1026 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
1029 s->tb_size = params->tb_size;
1030 s->tb_time = port->time;
1031 s->tb_credits = s->tb_size / 2;
1033 /* Traffic Classes (TCs) */
1034 s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
1035 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1036 if (params->qsize[i])
1037 s->tc_credits_per_period[i]
1038 = rte_sched_time_ms_to_bytes(params->tc_period,
1039 params->tc_rate[i]);
1041 s->tc_time = port->time + s->tc_period;
1042 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1043 if (params->qsize[i])
1044 s->tc_credits[i] = s->tc_credits_per_period[i];
1046 /* compile time checks */
1047 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1048 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1049 (RTE_SCHED_PORT_N_GRINDERS - 1));
1051 /* User parameters */
1052 s->n_pipes_per_subport_enabled = params->n_pipes_per_subport_enabled;
1053 memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1054 s->n_pipe_profiles = params->n_pipe_profiles;
1055 s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1057 #ifdef RTE_SCHED_RED
1058 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1061 for (j = 0; j < RTE_COLORS; j++) {
1062 /* if min/max are both zero, then RED is disabled */
1063 if ((params->red_params[i][j].min_th |
1064 params->red_params[i][j].max_th) == 0) {
1068 if (rte_red_config_init(&s->red_config[i][j],
1069 params->red_params[i][j].wq_log2,
1070 params->red_params[i][j].min_th,
1071 params->red_params[i][j].max_th,
1072 params->red_params[i][j].maxp_inv) != 0) {
1073 rte_sched_free_memory(port, n_subports);
1075 RTE_LOG(NOTICE, SCHED,
1076 "%s: RED configuration init fails\n", __func__);
1083 /* Scheduling loop detection */
1084 s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1085 s->pipe_exhaustion = 0;
1088 s->busy_grinders = 0;
1090 /* Queue base calculation */
1091 rte_sched_subport_config_qsize(s);
1093 /* Large data structures */
1094 s->pipe = (struct rte_sched_pipe *)
1095 (s->memory + rte_sched_subport_get_array_base(params,
1096 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1097 s->queue = (struct rte_sched_queue *)
1098 (s->memory + rte_sched_subport_get_array_base(params,
1099 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1100 s->queue_extra = (struct rte_sched_queue_extra *)
1101 (s->memory + rte_sched_subport_get_array_base(params,
1102 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1103 s->pipe_profiles = (struct rte_sched_pipe_profile *)
1104 (s->memory + rte_sched_subport_get_array_base(params,
1105 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1106 s->bmp_array = s->memory + rte_sched_subport_get_array_base(params,
1107 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1108 s->queue_array = (struct rte_mbuf **)
1109 (s->memory + rte_sched_subport_get_array_base(params,
1110 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1112 /* Pipe profile table */
1113 rte_sched_subport_config_pipe_profile_table(s, params, port->rate);
1116 n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1117 bmp_mem_size = rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
1118 s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1120 if (s->bmp == NULL) {
1122 "%s: Subport bitmap init error\n", __func__);
1124 rte_sched_free_memory(port, n_subports);
1128 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1129 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1131 #ifdef RTE_SCHED_SUBPORT_TC_OV
1132 /* TC oversubscription */
1133 s->tc_ov_wm_min = port->mtu;
1134 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
1135 s->pipe_tc_be_rate_max);
1136 s->tc_ov_wm = s->tc_ov_wm_max;
1137 s->tc_ov_period_id = 0;
1143 rte_sched_port_log_subport_config(port, subport_id);
1149 rte_sched_pipe_config(struct rte_sched_port *port,
1150 uint32_t subport_id,
1152 int32_t pipe_profile)
1154 struct rte_sched_subport *s;
1155 struct rte_sched_pipe *p;
1156 struct rte_sched_pipe_profile *params;
1157 uint32_t n_subports = subport_id + 1;
1158 uint32_t deactivate, profile, i;
1160 /* Check user parameters */
1161 profile = (uint32_t) pipe_profile;
1162 deactivate = (pipe_profile < 0);
1166 "%s: Incorrect value for parameter port\n", __func__);
1170 if (subport_id >= port->n_subports_per_port) {
1172 "%s: Incorrect value for parameter subport id\n", __func__);
1174 rte_sched_free_memory(port, n_subports);
1178 s = port->subports[subport_id];
1179 if (pipe_id >= s->n_pipes_per_subport_enabled) {
1181 "%s: Incorrect value for parameter pipe id\n", __func__);
1183 rte_sched_free_memory(port, n_subports);
1187 if (!deactivate && profile >= s->n_pipe_profiles) {
1189 "%s: Incorrect value for parameter pipe profile\n", __func__);
1191 rte_sched_free_memory(port, n_subports);
1195 /* Handle the case when pipe already has a valid configuration */
1196 p = s->pipe + pipe_id;
1198 params = s->pipe_profiles + p->profile;
1200 double subport_tc_be_rate =
1201 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1202 / (double) s->tc_period;
1203 double pipe_tc_be_rate =
1204 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1205 / (double) params->tc_period;
1206 uint32_t tc_be_ov = s->tc_ov;
1208 /* Unplug pipe from its subport */
1209 s->tc_ov_n -= params->tc_ov_weight;
1210 s->tc_ov_rate -= pipe_tc_be_rate;
1211 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1213 if (s->tc_ov != tc_be_ov) {
1214 RTE_LOG(DEBUG, SCHED,
1215 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1216 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1219 /* Reset the pipe */
1220 memset(p, 0, sizeof(struct rte_sched_pipe));
1226 /* Apply the new pipe configuration */
1227 p->profile = profile;
1228 params = s->pipe_profiles + p->profile;
1230 /* Token Bucket (TB) */
1231 p->tb_time = port->time;
1232 p->tb_credits = params->tb_size / 2;
1234 /* Traffic Classes (TCs) */
1235 p->tc_time = port->time + params->tc_period;
1237 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1239 p->tc_credits[i] = params->tc_credits_per_period[i];
1242 /* Subport best effort tc oversubscription */
1243 double subport_tc_be_rate =
1244 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1245 / (double) s->tc_period;
1246 double pipe_tc_be_rate =
1247 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1248 / (double) params->tc_period;
1249 uint32_t tc_be_ov = s->tc_ov;
1251 s->tc_ov_n += params->tc_ov_weight;
1252 s->tc_ov_rate += pipe_tc_be_rate;
1253 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1255 if (s->tc_ov != tc_be_ov) {
1256 RTE_LOG(DEBUG, SCHED,
1257 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1258 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1260 p->tc_ov_period_id = s->tc_ov_period_id;
1261 p->tc_ov_credits = s->tc_ov_wm;
1268 rte_sched_subport_pipe_profile_add(struct rte_sched_port *port,
1269 uint32_t subport_id,
1270 struct rte_sched_pipe_params *params,
1271 uint32_t *pipe_profile_id)
1273 struct rte_sched_subport *s;
1274 struct rte_sched_pipe_profile *pp;
1281 "%s: Incorrect value for parameter port\n", __func__);
1285 /* Subport id not exceeds the max limit */
1286 if (subport_id > port->n_subports_per_port) {
1288 "%s: Incorrect value for subport id\n", __func__);
1292 s = port->subports[subport_id];
1294 /* Pipe profiles exceeds the max limit */
1295 if (s->n_pipe_profiles >= s->n_max_pipe_profiles) {
1297 "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1302 status = pipe_profile_check(params, port->rate, &s->qsize[0]);
1305 "%s: Pipe profile check failed(%d)\n", __func__, status);
1309 pp = &s->pipe_profiles[s->n_pipe_profiles];
1310 rte_sched_pipe_profile_convert(s, params, pp, port->rate);
1312 /* Pipe profile should not exists */
1313 for (i = 0; i < s->n_pipe_profiles; i++)
1314 if (memcmp(s->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1316 "%s: Pipe profile exists\n", __func__);
1320 /* Pipe profile commit */
1321 *pipe_profile_id = s->n_pipe_profiles;
1322 s->n_pipe_profiles++;
1324 if (s->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1325 s->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1327 rte_sched_port_log_pipe_profile(s, *pipe_profile_id);
1332 static inline uint32_t
1333 rte_sched_port_qindex(struct rte_sched_port *port,
1336 uint32_t traffic_class,
1339 return ((subport & (port->n_subports_per_port - 1)) <<
1340 (port->n_pipes_per_subport_log2 + 4)) |
1342 (port->subports[subport]->n_pipes_per_subport_enabled - 1)) << 4) |
1343 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1344 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1348 rte_sched_port_pkt_write(struct rte_sched_port *port,
1349 struct rte_mbuf *pkt,
1350 uint32_t subport, uint32_t pipe,
1351 uint32_t traffic_class,
1352 uint32_t queue, enum rte_color color)
1355 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1357 rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1361 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1362 const struct rte_mbuf *pkt,
1363 uint32_t *subport, uint32_t *pipe,
1364 uint32_t *traffic_class, uint32_t *queue)
1366 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1368 *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1369 *pipe = (queue_id >> 4) &
1370 (port->subports[*subport]->n_pipes_per_subport_enabled - 1);
1371 *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1372 *queue = rte_sched_port_tc_queue(port, queue_id);
1376 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1378 return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1382 rte_sched_subport_read_stats(struct rte_sched_port *port,
1383 uint32_t subport_id,
1384 struct rte_sched_subport_stats *stats,
1387 struct rte_sched_subport *s;
1389 /* Check user parameters */
1392 "%s: Incorrect value for parameter port\n", __func__);
1396 if (subport_id >= port->n_subports_per_port) {
1398 "%s: Incorrect value for subport id\n", __func__);
1402 if (stats == NULL) {
1404 "%s: Incorrect value for parameter stats\n", __func__);
1408 if (tc_ov == NULL) {
1410 "%s: Incorrect value for tc_ov\n", __func__);
1414 s = port->subports[subport_id];
1416 /* Copy subport stats and clear */
1417 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1418 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1420 /* Subport TC oversubscription status */
1427 rte_sched_queue_read_stats(struct rte_sched_port *port,
1429 struct rte_sched_queue_stats *stats,
1432 struct rte_sched_subport *s;
1433 struct rte_sched_queue *q;
1434 struct rte_sched_queue_extra *qe;
1435 uint32_t subport_id, subport_qmask, subport_qindex;
1437 /* Check user parameters */
1440 "%s: Incorrect value for parameter port\n", __func__);
1444 if (queue_id >= rte_sched_port_queues_per_port(port)) {
1446 "%s: Incorrect value for queue id\n", __func__);
1450 if (stats == NULL) {
1452 "%s: Incorrect value for parameter stats\n", __func__);
1458 "%s: Incorrect value for parameter qlen\n", __func__);
1461 subport_qmask = port->n_pipes_per_subport_log2 + 4;
1462 subport_id = (queue_id >> subport_qmask) & (port->n_subports_per_port - 1);
1464 s = port->subports[subport_id];
1465 subport_qindex = ((1 << subport_qmask) - 1) & queue_id;
1466 q = s->queue + subport_qindex;
1467 qe = s->queue_extra + subport_qindex;
1469 /* Copy queue stats and clear */
1470 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1471 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1474 *qlen = q->qw - q->qr;
1479 #ifdef RTE_SCHED_DEBUG
1482 rte_sched_port_queue_is_empty(struct rte_sched_subport *subport,
1485 struct rte_sched_queue *queue = subport->queue + qindex;
1487 return queue->qr == queue->qw;
1490 #endif /* RTE_SCHED_DEBUG */
1492 #ifdef RTE_SCHED_COLLECT_STATS
1495 rte_sched_port_update_subport_stats(struct rte_sched_port *port,
1496 struct rte_sched_subport *subport,
1498 struct rte_mbuf *pkt)
1500 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1501 uint32_t pkt_len = pkt->pkt_len;
1503 subport->stats.n_pkts_tc[tc_index] += 1;
1504 subport->stats.n_bytes_tc[tc_index] += pkt_len;
1507 #ifdef RTE_SCHED_RED
1509 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1510 struct rte_sched_subport *subport,
1512 struct rte_mbuf *pkt,
1516 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1517 struct rte_sched_subport *subport,
1519 struct rte_mbuf *pkt,
1520 __rte_unused uint32_t red)
1523 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1524 uint32_t pkt_len = pkt->pkt_len;
1526 subport->stats.n_pkts_tc_dropped[tc_index] += 1;
1527 subport->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1528 #ifdef RTE_SCHED_RED
1529 subport->stats.n_pkts_red_dropped[tc_index] += red;
1534 rte_sched_port_update_queue_stats(struct rte_sched_subport *subport,
1536 struct rte_mbuf *pkt)
1538 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1539 uint32_t pkt_len = pkt->pkt_len;
1541 qe->stats.n_pkts += 1;
1542 qe->stats.n_bytes += pkt_len;
1545 #ifdef RTE_SCHED_RED
1547 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1549 struct rte_mbuf *pkt,
1553 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1555 struct rte_mbuf *pkt,
1556 __rte_unused uint32_t red)
1559 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1560 uint32_t pkt_len = pkt->pkt_len;
1562 qe->stats.n_pkts_dropped += 1;
1563 qe->stats.n_bytes_dropped += pkt_len;
1564 #ifdef RTE_SCHED_RED
1565 qe->stats.n_pkts_red_dropped += red;
1569 #endif /* RTE_SCHED_COLLECT_STATS */
1571 #ifdef RTE_SCHED_RED
1574 rte_sched_port_red_drop(struct rte_sched_port *port,
1575 struct rte_sched_subport *subport,
1576 struct rte_mbuf *pkt,
1580 struct rte_sched_queue_extra *qe;
1581 struct rte_red_config *red_cfg;
1582 struct rte_red *red;
1584 enum rte_color color;
1586 tc_index = rte_sched_port_pipe_tc(port, qindex);
1587 color = rte_sched_port_pkt_read_color(pkt);
1588 red_cfg = &subport->red_config[tc_index][color];
1590 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1593 qe = subport->queue_extra + qindex;
1596 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1600 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port,
1601 struct rte_sched_subport *subport, uint32_t qindex)
1603 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1604 struct rte_red *red = &qe->red;
1606 rte_red_mark_queue_empty(red, port->time);
1611 static inline int rte_sched_port_red_drop(struct rte_sched_port *port __rte_unused,
1612 struct rte_sched_subport *subport __rte_unused,
1613 struct rte_mbuf *pkt __rte_unused,
1614 uint32_t qindex __rte_unused,
1615 uint16_t qlen __rte_unused)
1620 #define rte_sched_port_set_queue_empty_timestamp(port, subport, qindex)
1622 #endif /* RTE_SCHED_RED */
1624 #ifdef RTE_SCHED_DEBUG
1627 debug_check_queue_slab(struct rte_sched_subport *subport, uint32_t bmp_pos,
1634 rte_panic("Empty slab at position %u\n", bmp_pos);
1637 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1638 if (mask & bmp_slab) {
1639 if (rte_sched_port_queue_is_empty(subport, bmp_pos + i)) {
1640 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1647 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1651 #endif /* RTE_SCHED_DEBUG */
1653 static inline struct rte_sched_subport *
1654 rte_sched_port_subport(struct rte_sched_port *port,
1655 struct rte_mbuf *pkt)
1657 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1658 uint32_t subport_id = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1660 return port->subports[subport_id];
1663 static inline uint32_t
1664 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_subport *subport,
1665 struct rte_mbuf *pkt, uint32_t subport_qmask)
1667 struct rte_sched_queue *q;
1668 #ifdef RTE_SCHED_COLLECT_STATS
1669 struct rte_sched_queue_extra *qe;
1671 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1672 uint32_t subport_queue_id = subport_qmask & qindex;
1674 q = subport->queue + subport_queue_id;
1676 #ifdef RTE_SCHED_COLLECT_STATS
1677 qe = subport->queue_extra + subport_queue_id;
1681 return subport_queue_id;
1685 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1686 struct rte_sched_subport *subport,
1688 struct rte_mbuf **qbase)
1690 struct rte_sched_queue *q;
1691 struct rte_mbuf **q_qw;
1694 q = subport->queue + qindex;
1695 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1696 q_qw = qbase + (q->qw & (qsize - 1));
1698 rte_prefetch0(q_qw);
1699 rte_bitmap_prefetch0(subport->bmp, qindex);
1703 rte_sched_port_enqueue_qwa(struct rte_sched_port *port,
1704 struct rte_sched_subport *subport,
1706 struct rte_mbuf **qbase,
1707 struct rte_mbuf *pkt)
1709 struct rte_sched_queue *q;
1713 q = subport->queue + qindex;
1714 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1715 qlen = q->qw - q->qr;
1717 /* Drop the packet (and update drop stats) when queue is full */
1718 if (unlikely(rte_sched_port_red_drop(port, subport, pkt, qindex, qlen) ||
1720 rte_pktmbuf_free(pkt);
1721 #ifdef RTE_SCHED_COLLECT_STATS
1722 rte_sched_port_update_subport_stats_on_drop(port, subport,
1723 qindex, pkt, qlen < qsize);
1724 rte_sched_port_update_queue_stats_on_drop(subport, qindex, pkt,
1730 /* Enqueue packet */
1731 qbase[q->qw & (qsize - 1)] = pkt;
1734 /* Activate queue in the subport bitmap */
1735 rte_bitmap_set(subport->bmp, qindex);
1738 #ifdef RTE_SCHED_COLLECT_STATS
1739 rte_sched_port_update_subport_stats(port, subport, qindex, pkt);
1740 rte_sched_port_update_queue_stats(subport, qindex, pkt);
1748 * The enqueue function implements a 4-level pipeline with each stage
1749 * processing two different packets. The purpose of using a pipeline
1750 * is to hide the latency of prefetching the data structures. The
1751 * naming convention is presented in the diagram below:
1753 * p00 _______ p10 _______ p20 _______ p30 _______
1754 * ----->| |----->| |----->| |----->| |----->
1755 * | 0 | | 1 | | 2 | | 3 |
1756 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1761 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1764 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1765 *pkt30, *pkt31, *pkt_last;
1766 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1767 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1768 struct rte_sched_subport *subport00, *subport01, *subport10, *subport11,
1769 *subport20, *subport21, *subport30, *subport31, *subport_last;
1770 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1771 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1772 uint32_t subport_qmask;
1776 subport_qmask = (1 << (port->n_pipes_per_subport_log2 + 4)) - 1;
1779 * Less then 6 input packets available, which is not enough to
1782 if (unlikely(n_pkts < 6)) {
1783 struct rte_sched_subport *subports[5];
1784 struct rte_mbuf **q_base[5];
1787 /* Prefetch the mbuf structure of each packet */
1788 for (i = 0; i < n_pkts; i++)
1789 rte_prefetch0(pkts[i]);
1791 /* Prefetch the subport structure for each packet */
1792 for (i = 0; i < n_pkts; i++)
1793 subports[i] = rte_sched_port_subport(port, pkts[i]);
1795 /* Prefetch the queue structure for each queue */
1796 for (i = 0; i < n_pkts; i++)
1797 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(subports[i],
1798 pkts[i], subport_qmask);
1800 /* Prefetch the write pointer location of each queue */
1801 for (i = 0; i < n_pkts; i++) {
1802 q_base[i] = rte_sched_subport_pipe_qbase(subports[i], q[i]);
1803 rte_sched_port_enqueue_qwa_prefetch0(port, subports[i],
1807 /* Write each packet to its queue */
1808 for (i = 0; i < n_pkts; i++)
1809 result += rte_sched_port_enqueue_qwa(port, subports[i],
1810 q[i], q_base[i], pkts[i]);
1815 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1818 rte_prefetch0(pkt20);
1819 rte_prefetch0(pkt21);
1823 rte_prefetch0(pkt10);
1824 rte_prefetch0(pkt11);
1826 subport20 = rte_sched_port_subport(port, pkt20);
1827 subport21 = rte_sched_port_subport(port, pkt21);
1828 q20 = rte_sched_port_enqueue_qptrs_prefetch0(subport20,
1829 pkt20, subport_qmask);
1830 q21 = rte_sched_port_enqueue_qptrs_prefetch0(subport21,
1831 pkt21, subport_qmask);
1835 rte_prefetch0(pkt00);
1836 rte_prefetch0(pkt01);
1838 subport10 = rte_sched_port_subport(port, pkt10);
1839 subport11 = rte_sched_port_subport(port, pkt11);
1840 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
1841 pkt10, subport_qmask);
1842 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
1843 pkt11, subport_qmask);
1845 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
1846 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
1847 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
1848 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
1850 /* Run the pipeline */
1851 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1852 /* Propagate stage inputs */
1863 subport30 = subport20;
1864 subport31 = subport21;
1865 subport20 = subport10;
1866 subport21 = subport11;
1867 q30_base = q20_base;
1868 q31_base = q21_base;
1870 /* Stage 0: Get packets in */
1872 pkt01 = pkts[i + 1];
1873 rte_prefetch0(pkt00);
1874 rte_prefetch0(pkt01);
1876 /* Stage 1: Prefetch subport and queue structure storing queue pointers */
1877 subport10 = rte_sched_port_subport(port, pkt10);
1878 subport11 = rte_sched_port_subport(port, pkt11);
1879 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
1880 pkt10, subport_qmask);
1881 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
1882 pkt11, subport_qmask);
1884 /* Stage 2: Prefetch queue write location */
1885 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
1886 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
1887 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
1888 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
1890 /* Stage 3: Write packet to queue and activate queue */
1891 r30 = rte_sched_port_enqueue_qwa(port, subport30,
1892 q30, q30_base, pkt30);
1893 r31 = rte_sched_port_enqueue_qwa(port, subport31,
1894 q31, q31_base, pkt31);
1895 result += r30 + r31;
1899 * Drain the pipeline (exactly 6 packets).
1900 * Handle the last packet in the case
1901 * of an odd number of input packets.
1903 pkt_last = pkts[n_pkts - 1];
1904 rte_prefetch0(pkt_last);
1906 subport00 = rte_sched_port_subport(port, pkt00);
1907 subport01 = rte_sched_port_subport(port, pkt01);
1908 q00 = rte_sched_port_enqueue_qptrs_prefetch0(subport00,
1909 pkt00, subport_qmask);
1910 q01 = rte_sched_port_enqueue_qptrs_prefetch0(subport01,
1911 pkt01, subport_qmask);
1913 q10_base = rte_sched_subport_pipe_qbase(subport10, q10);
1914 q11_base = rte_sched_subport_pipe_qbase(subport11, q11);
1915 rte_sched_port_enqueue_qwa_prefetch0(port, subport10, q10, q10_base);
1916 rte_sched_port_enqueue_qwa_prefetch0(port, subport11, q11, q11_base);
1918 r20 = rte_sched_port_enqueue_qwa(port, subport20,
1919 q20, q20_base, pkt20);
1920 r21 = rte_sched_port_enqueue_qwa(port, subport21,
1921 q21, q21_base, pkt21);
1922 result += r20 + r21;
1924 subport_last = rte_sched_port_subport(port, pkt_last);
1925 q_last = rte_sched_port_enqueue_qptrs_prefetch0(subport_last,
1926 pkt_last, subport_qmask);
1928 q00_base = rte_sched_subport_pipe_qbase(subport00, q00);
1929 q01_base = rte_sched_subport_pipe_qbase(subport01, q01);
1930 rte_sched_port_enqueue_qwa_prefetch0(port, subport00, q00, q00_base);
1931 rte_sched_port_enqueue_qwa_prefetch0(port, subport01, q01, q01_base);
1933 r10 = rte_sched_port_enqueue_qwa(port, subport10, q10,
1935 r11 = rte_sched_port_enqueue_qwa(port, subport11, q11,
1937 result += r10 + r11;
1939 q_last_base = rte_sched_subport_pipe_qbase(subport_last, q_last);
1940 rte_sched_port_enqueue_qwa_prefetch0(port, subport_last,
1941 q_last, q_last_base);
1943 r00 = rte_sched_port_enqueue_qwa(port, subport00, q00,
1945 r01 = rte_sched_port_enqueue_qwa(port, subport01, q01,
1947 result += r00 + r01;
1950 r_last = rte_sched_port_enqueue_qwa(port, subport_last,
1951 q_last, q_last_base, pkt_last);
1958 #ifndef RTE_SCHED_SUBPORT_TC_OV
1961 grinder_credits_update(struct rte_sched_port *port,
1962 struct rte_sched_subport *subport, uint32_t pos)
1964 struct rte_sched_grinder *grinder = subport->grinder + pos;
1965 struct rte_sched_pipe *pipe = grinder->pipe;
1966 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1971 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1972 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1973 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1974 subport->tb_time += n_periods * subport->tb_period;
1977 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1978 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1979 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1980 pipe->tb_time += n_periods * params->tb_period;
1983 if (unlikely(port->time >= subport->tc_time)) {
1984 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1985 subport->tc_credits[i] = subport->tc_credits_per_period[i];
1987 subport->tc_time = port->time + subport->tc_period;
1991 if (unlikely(port->time >= pipe->tc_time)) {
1992 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1993 pipe->tc_credits[i] = params->tc_credits_per_period[i];
1995 pipe->tc_time = port->time + params->tc_period;
2001 static inline uint32_t
2002 grinder_tc_ov_credits_update(struct rte_sched_port *port,
2003 struct rte_sched_subport *subport)
2005 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2006 uint32_t tc_consumption = 0, tc_ov_consumption_max;
2007 uint32_t tc_ov_wm = subport->tc_ov_wm;
2010 if (subport->tc_ov == 0)
2011 return subport->tc_ov_wm_max;
2013 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2014 tc_ov_consumption[i] =
2015 subport->tc_credits_per_period[i] - subport->tc_credits[i];
2016 tc_consumption += tc_ov_consumption[i];
2019 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
2020 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2021 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2023 tc_ov_consumption_max =
2024 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2027 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2028 (tc_ov_consumption_max - port->mtu)) {
2029 tc_ov_wm -= tc_ov_wm >> 7;
2030 if (tc_ov_wm < subport->tc_ov_wm_min)
2031 tc_ov_wm = subport->tc_ov_wm_min;
2036 tc_ov_wm += (tc_ov_wm >> 7) + 1;
2037 if (tc_ov_wm > subport->tc_ov_wm_max)
2038 tc_ov_wm = subport->tc_ov_wm_max;
2044 grinder_credits_update(struct rte_sched_port *port,
2045 struct rte_sched_subport *subport, uint32_t pos)
2047 struct rte_sched_grinder *grinder = subport->grinder + pos;
2048 struct rte_sched_pipe *pipe = grinder->pipe;
2049 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2054 n_periods = (port->time - subport->tb_time) / subport->tb_period;
2055 subport->tb_credits += n_periods * subport->tb_credits_per_period;
2056 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
2057 subport->tb_time += n_periods * subport->tb_period;
2060 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2061 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2062 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
2063 pipe->tb_time += n_periods * params->tb_period;
2066 if (unlikely(port->time >= subport->tc_time)) {
2067 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, subport);
2069 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2070 subport->tc_credits[i] = subport->tc_credits_per_period[i];
2072 subport->tc_time = port->time + subport->tc_period;
2073 subport->tc_ov_period_id++;
2077 if (unlikely(port->time >= pipe->tc_time)) {
2078 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2079 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2080 pipe->tc_time = port->time + params->tc_period;
2083 /* Pipe TCs - Oversubscription */
2084 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2085 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2087 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2091 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
2094 #ifndef RTE_SCHED_SUBPORT_TC_OV
2097 grinder_credits_check(struct rte_sched_port *port,
2098 struct rte_sched_subport *subport, uint32_t pos)
2100 struct rte_sched_grinder *grinder = subport->grinder + pos;
2101 struct rte_sched_pipe *pipe = grinder->pipe;
2102 struct rte_mbuf *pkt = grinder->pkt;
2103 uint32_t tc_index = grinder->tc_index;
2104 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2105 uint32_t subport_tb_credits = subport->tb_credits;
2106 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2107 uint32_t pipe_tb_credits = pipe->tb_credits;
2108 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2111 /* Check queue credits */
2112 enough_credits = (pkt_len <= subport_tb_credits) &&
2113 (pkt_len <= subport_tc_credits) &&
2114 (pkt_len <= pipe_tb_credits) &&
2115 (pkt_len <= pipe_tc_credits);
2117 if (!enough_credits)
2120 /* Update port credits */
2121 subport->tb_credits -= pkt_len;
2122 subport->tc_credits[tc_index] -= pkt_len;
2123 pipe->tb_credits -= pkt_len;
2124 pipe->tc_credits[tc_index] -= pkt_len;
2132 grinder_credits_check(struct rte_sched_port *port,
2133 struct rte_sched_subport *subport, uint32_t pos)
2135 struct rte_sched_grinder *grinder = subport->grinder + pos;
2136 struct rte_sched_pipe *pipe = grinder->pipe;
2137 struct rte_mbuf *pkt = grinder->pkt;
2138 uint32_t tc_index = grinder->tc_index;
2139 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2140 uint32_t subport_tb_credits = subport->tb_credits;
2141 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2142 uint32_t pipe_tb_credits = pipe->tb_credits;
2143 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2144 uint32_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2145 uint32_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2146 uint32_t pipe_tc_ov_credits, i;
2149 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2150 pipe_tc_ov_mask1[i] = UINT32_MAX;
2152 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2153 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = UINT32_MAX;
2154 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2156 /* Check pipe and subport credits */
2157 enough_credits = (pkt_len <= subport_tb_credits) &&
2158 (pkt_len <= subport_tc_credits) &&
2159 (pkt_len <= pipe_tb_credits) &&
2160 (pkt_len <= pipe_tc_credits) &&
2161 (pkt_len <= pipe_tc_ov_credits);
2163 if (!enough_credits)
2166 /* Update pipe and subport credits */
2167 subport->tb_credits -= pkt_len;
2168 subport->tc_credits[tc_index] -= pkt_len;
2169 pipe->tb_credits -= pkt_len;
2170 pipe->tc_credits[tc_index] -= pkt_len;
2171 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2176 #endif /* RTE_SCHED_SUBPORT_TC_OV */
2180 grinder_schedule(struct rte_sched_port *port,
2181 struct rte_sched_subport *subport, uint32_t pos)
2183 struct rte_sched_grinder *grinder = subport->grinder + pos;
2184 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2185 struct rte_mbuf *pkt = grinder->pkt;
2186 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2187 uint32_t be_tc_active;
2189 if (!grinder_credits_check(port, subport, pos))
2192 /* Advance port time */
2193 port->time += pkt_len;
2196 port->pkts_out[port->n_pkts_out++] = pkt;
2199 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2200 grinder->wrr_tokens[grinder->qpos] +=
2201 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2203 if (queue->qr == queue->qw) {
2204 uint32_t qindex = grinder->qindex[grinder->qpos];
2206 rte_bitmap_clear(subport->bmp, qindex);
2207 grinder->qmask &= ~(1 << grinder->qpos);
2209 grinder->wrr_mask[grinder->qpos] = 0;
2210 rte_sched_port_set_queue_empty_timestamp(port, subport, qindex);
2213 /* Reset pipe loop detection */
2214 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2215 grinder->productive = 1;
2220 #ifdef SCHED_VECTOR_SSE4
2223 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2225 __m128i index = _mm_set1_epi32(base_pipe);
2226 __m128i pipes = _mm_load_si128((__m128i *)subport->grinder_base_bmp_pos);
2227 __m128i res = _mm_cmpeq_epi32(pipes, index);
2229 pipes = _mm_load_si128((__m128i *)(subport->grinder_base_bmp_pos + 4));
2230 pipes = _mm_cmpeq_epi32(pipes, index);
2231 res = _mm_or_si128(res, pipes);
2233 if (_mm_testz_si128(res, res))
2239 #elif defined(SCHED_VECTOR_NEON)
2242 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2244 uint32x4_t index, pipes;
2245 uint32_t *pos = (uint32_t *)subport->grinder_base_bmp_pos;
2247 index = vmovq_n_u32(base_pipe);
2248 pipes = vld1q_u32(pos);
2249 if (!vminvq_u32(veorq_u32(pipes, index)))
2252 pipes = vld1q_u32(pos + 4);
2253 if (!vminvq_u32(veorq_u32(pipes, index)))
2262 grinder_pipe_exists(struct rte_sched_subport *subport, uint32_t base_pipe)
2266 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2267 if (subport->grinder_base_bmp_pos[i] == base_pipe)
2274 #endif /* RTE_SCHED_OPTIMIZATIONS */
2277 grinder_pcache_populate(struct rte_sched_subport *subport,
2278 uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2280 struct rte_sched_grinder *grinder = subport->grinder + pos;
2283 grinder->pcache_w = 0;
2284 grinder->pcache_r = 0;
2286 w[0] = (uint16_t) bmp_slab;
2287 w[1] = (uint16_t) (bmp_slab >> 16);
2288 w[2] = (uint16_t) (bmp_slab >> 32);
2289 w[3] = (uint16_t) (bmp_slab >> 48);
2291 grinder->pcache_qmask[grinder->pcache_w] = w[0];
2292 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2293 grinder->pcache_w += (w[0] != 0);
2295 grinder->pcache_qmask[grinder->pcache_w] = w[1];
2296 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2297 grinder->pcache_w += (w[1] != 0);
2299 grinder->pcache_qmask[grinder->pcache_w] = w[2];
2300 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2301 grinder->pcache_w += (w[2] != 0);
2303 grinder->pcache_qmask[grinder->pcache_w] = w[3];
2304 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2305 grinder->pcache_w += (w[3] != 0);
2309 grinder_tccache_populate(struct rte_sched_subport *subport,
2310 uint32_t pos, uint32_t qindex, uint16_t qmask)
2312 struct rte_sched_grinder *grinder = subport->grinder + pos;
2315 grinder->tccache_w = 0;
2316 grinder->tccache_r = 0;
2318 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2319 b = (uint8_t) ((qmask >> i) & 0x1);
2320 grinder->tccache_qmask[grinder->tccache_w] = b;
2321 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2322 grinder->tccache_w += (b != 0);
2325 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2326 grinder->tccache_qmask[grinder->tccache_w] = b;
2327 grinder->tccache_qindex[grinder->tccache_w] = qindex +
2328 RTE_SCHED_TRAFFIC_CLASS_BE;
2329 grinder->tccache_w += (b != 0);
2333 grinder_next_tc(struct rte_sched_port *port,
2334 struct rte_sched_subport *subport, uint32_t pos)
2336 struct rte_sched_grinder *grinder = subport->grinder + pos;
2337 struct rte_mbuf **qbase;
2341 if (grinder->tccache_r == grinder->tccache_w)
2344 qindex = grinder->tccache_qindex[grinder->tccache_r];
2345 qbase = rte_sched_subport_pipe_qbase(subport, qindex);
2346 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
2348 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2349 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2350 grinder->qsize = qsize;
2352 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2353 grinder->queue[0] = subport->queue + qindex;
2354 grinder->qbase[0] = qbase;
2355 grinder->qindex[0] = qindex;
2356 grinder->tccache_r++;
2361 grinder->queue[0] = subport->queue + qindex;
2362 grinder->queue[1] = subport->queue + qindex + 1;
2363 grinder->queue[2] = subport->queue + qindex + 2;
2364 grinder->queue[3] = subport->queue + qindex + 3;
2366 grinder->qbase[0] = qbase;
2367 grinder->qbase[1] = qbase + qsize;
2368 grinder->qbase[2] = qbase + 2 * qsize;
2369 grinder->qbase[3] = qbase + 3 * qsize;
2371 grinder->qindex[0] = qindex;
2372 grinder->qindex[1] = qindex + 1;
2373 grinder->qindex[2] = qindex + 2;
2374 grinder->qindex[3] = qindex + 3;
2376 grinder->tccache_r++;
2381 grinder_next_pipe(struct rte_sched_port *port,
2382 struct rte_sched_subport *subport, uint32_t pos)
2384 struct rte_sched_grinder *grinder = subport->grinder + pos;
2385 uint32_t pipe_qindex;
2386 uint16_t pipe_qmask;
2388 if (grinder->pcache_r < grinder->pcache_w) {
2389 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2390 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2391 grinder->pcache_r++;
2393 uint64_t bmp_slab = 0;
2394 uint32_t bmp_pos = 0;
2396 /* Get another non-empty pipe group */
2397 if (unlikely(rte_bitmap_scan(subport->bmp, &bmp_pos, &bmp_slab) <= 0))
2400 #ifdef RTE_SCHED_DEBUG
2401 debug_check_queue_slab(subport, bmp_pos, bmp_slab);
2404 /* Return if pipe group already in one of the other grinders */
2405 subport->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2406 if (unlikely(grinder_pipe_exists(subport, bmp_pos)))
2409 subport->grinder_base_bmp_pos[pos] = bmp_pos;
2411 /* Install new pipe group into grinder's pipe cache */
2412 grinder_pcache_populate(subport, pos, bmp_pos, bmp_slab);
2414 pipe_qmask = grinder->pcache_qmask[0];
2415 pipe_qindex = grinder->pcache_qindex[0];
2416 grinder->pcache_r = 1;
2419 /* Install new pipe in the grinder */
2420 grinder->pindex = pipe_qindex >> 4;
2421 grinder->subport = subport;
2422 grinder->pipe = subport->pipe + grinder->pindex;
2423 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2424 grinder->productive = 0;
2426 grinder_tccache_populate(subport, pos, pipe_qindex, pipe_qmask);
2427 grinder_next_tc(port, subport, pos);
2429 /* Check for pipe exhaustion */
2430 if (grinder->pindex == subport->pipe_loop) {
2431 subport->pipe_exhaustion = 1;
2432 subport->pipe_loop = RTE_SCHED_PIPE_INVALID;
2440 grinder_wrr_load(struct rte_sched_subport *subport, uint32_t pos)
2442 struct rte_sched_grinder *grinder = subport->grinder + pos;
2443 struct rte_sched_pipe *pipe = grinder->pipe;
2444 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2445 uint32_t qmask = grinder->qmask;
2447 grinder->wrr_tokens[0] =
2448 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2449 grinder->wrr_tokens[1] =
2450 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2451 grinder->wrr_tokens[2] =
2452 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2453 grinder->wrr_tokens[3] =
2454 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2456 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2457 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2458 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2459 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2461 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2462 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2463 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2464 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2468 grinder_wrr_store(struct rte_sched_subport *subport, uint32_t pos)
2470 struct rte_sched_grinder *grinder = subport->grinder + pos;
2471 struct rte_sched_pipe *pipe = grinder->pipe;
2473 pipe->wrr_tokens[0] =
2474 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2475 RTE_SCHED_WRR_SHIFT;
2476 pipe->wrr_tokens[1] =
2477 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2478 RTE_SCHED_WRR_SHIFT;
2479 pipe->wrr_tokens[2] =
2480 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2481 RTE_SCHED_WRR_SHIFT;
2482 pipe->wrr_tokens[3] =
2483 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2484 RTE_SCHED_WRR_SHIFT;
2488 grinder_wrr(struct rte_sched_subport *subport, uint32_t pos)
2490 struct rte_sched_grinder *grinder = subport->grinder + pos;
2491 uint16_t wrr_tokens_min;
2493 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2494 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2495 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2496 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2498 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2499 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2501 grinder->wrr_tokens[0] -= wrr_tokens_min;
2502 grinder->wrr_tokens[1] -= wrr_tokens_min;
2503 grinder->wrr_tokens[2] -= wrr_tokens_min;
2504 grinder->wrr_tokens[3] -= wrr_tokens_min;
2508 #define grinder_evict(subport, pos)
2511 grinder_prefetch_pipe(struct rte_sched_subport *subport, uint32_t pos)
2513 struct rte_sched_grinder *grinder = subport->grinder + pos;
2515 rte_prefetch0(grinder->pipe);
2516 rte_prefetch0(grinder->queue[0]);
2520 grinder_prefetch_tc_queue_arrays(struct rte_sched_subport *subport, uint32_t pos)
2522 struct rte_sched_grinder *grinder = subport->grinder + pos;
2523 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2525 qsize = grinder->qsize;
2528 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2529 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2531 rte_prefetch0(grinder->qbase[0] + qr[0]);
2535 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2536 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2537 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2538 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2540 rte_prefetch0(grinder->qbase[0] + qr[0]);
2541 rte_prefetch0(grinder->qbase[1] + qr[1]);
2543 grinder_wrr_load(subport, pos);
2544 grinder_wrr(subport, pos);
2546 rte_prefetch0(grinder->qbase[2] + qr[2]);
2547 rte_prefetch0(grinder->qbase[3] + qr[3]);
2551 grinder_prefetch_mbuf(struct rte_sched_subport *subport, uint32_t pos)
2553 struct rte_sched_grinder *grinder = subport->grinder + pos;
2554 uint32_t qpos = grinder->qpos;
2555 struct rte_mbuf **qbase = grinder->qbase[qpos];
2556 uint16_t qsize = grinder->qsize;
2557 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2559 grinder->pkt = qbase[qr];
2560 rte_prefetch0(grinder->pkt);
2562 if (unlikely((qr & 0x7) == 7)) {
2563 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2565 rte_prefetch0(qbase + qr_next);
2569 static inline uint32_t
2570 grinder_handle(struct rte_sched_port *port,
2571 struct rte_sched_subport *subport, uint32_t pos)
2573 struct rte_sched_grinder *grinder = subport->grinder + pos;
2575 switch (grinder->state) {
2576 case e_GRINDER_PREFETCH_PIPE:
2578 if (grinder_next_pipe(port, subport, pos)) {
2579 grinder_prefetch_pipe(subport, pos);
2580 subport->busy_grinders++;
2582 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2589 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2591 struct rte_sched_pipe *pipe = grinder->pipe;
2593 grinder->pipe_params = subport->pipe_profiles + pipe->profile;
2594 grinder_prefetch_tc_queue_arrays(subport, pos);
2595 grinder_credits_update(port, subport, pos);
2597 grinder->state = e_GRINDER_PREFETCH_MBUF;
2601 case e_GRINDER_PREFETCH_MBUF:
2603 grinder_prefetch_mbuf(subport, pos);
2605 grinder->state = e_GRINDER_READ_MBUF;
2609 case e_GRINDER_READ_MBUF:
2611 uint32_t wrr_active, result = 0;
2613 result = grinder_schedule(port, subport, pos);
2615 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2617 /* Look for next packet within the same TC */
2618 if (result && grinder->qmask) {
2620 grinder_wrr(subport, pos);
2622 grinder_prefetch_mbuf(subport, pos);
2628 grinder_wrr_store(subport, pos);
2630 /* Look for another active TC within same pipe */
2631 if (grinder_next_tc(port, subport, pos)) {
2632 grinder_prefetch_tc_queue_arrays(subport, pos);
2634 grinder->state = e_GRINDER_PREFETCH_MBUF;
2638 if (grinder->productive == 0 &&
2639 subport->pipe_loop == RTE_SCHED_PIPE_INVALID)
2640 subport->pipe_loop = grinder->pindex;
2642 grinder_evict(subport, pos);
2644 /* Look for another active pipe */
2645 if (grinder_next_pipe(port, subport, pos)) {
2646 grinder_prefetch_pipe(subport, pos);
2648 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2652 /* No active pipe found */
2653 subport->busy_grinders--;
2655 grinder->state = e_GRINDER_PREFETCH_PIPE;
2660 rte_panic("Algorithmic error (invalid state)\n");
2666 rte_sched_port_time_resync(struct rte_sched_port *port)
2668 uint64_t cycles = rte_get_tsc_cycles();
2669 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2670 uint64_t bytes_diff;
2673 /* Compute elapsed time in bytes */
2674 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2675 port->inv_cycles_per_byte);
2677 /* Advance port time */
2678 port->time_cpu_cycles = cycles;
2679 port->time_cpu_bytes += bytes_diff;
2680 if (port->time < port->time_cpu_bytes)
2681 port->time = port->time_cpu_bytes;
2683 /* Reset pipe loop detection */
2684 for (i = 0; i < port->n_subports_per_port; i++)
2685 port->subports[i]->pipe_loop = RTE_SCHED_PIPE_INVALID;
2689 rte_sched_port_exceptions(struct rte_sched_subport *subport, int second_pass)
2693 /* Check if any exception flag is set */
2694 exceptions = (second_pass && subport->busy_grinders == 0) ||
2695 (subport->pipe_exhaustion == 1);
2697 /* Clear exception flags */
2698 subport->pipe_exhaustion = 0;
2704 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2706 struct rte_sched_subport *subport;
2707 uint32_t subport_id = port->subport_id;
2708 uint32_t i, n_subports = 0, count;
2710 port->pkts_out = pkts;
2711 port->n_pkts_out = 0;
2713 rte_sched_port_time_resync(port);
2715 /* Take each queue in the grinder one step further */
2716 for (i = 0, count = 0; ; i++) {
2717 subport = port->subports[subport_id];
2719 count += grinder_handle(port, subport,
2720 i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2722 if (count == n_pkts) {
2725 if (subport_id == port->n_subports_per_port)
2728 port->subport_id = subport_id;
2732 if (rte_sched_port_exceptions(subport, i >= RTE_SCHED_PORT_N_GRINDERS)) {
2738 if (subport_id == port->n_subports_per_port)
2741 if (n_subports == port->n_subports_per_port) {
2742 port->subport_id = subport_id;