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_port_get_array_base(struct rte_sched_port_params *params, enum rte_sched_port_array array)
482 uint32_t n_subports_per_port = params->n_subports_per_port;
483 uint32_t n_pipes_per_subport = params->n_pipes_per_subport;
484 uint32_t n_pipes_per_port = n_pipes_per_subport * n_subports_per_port;
485 uint32_t n_queues_per_port = RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport * n_subports_per_port;
487 uint32_t size_subport = n_subports_per_port * sizeof(struct rte_sched_subport);
488 uint32_t size_pipe = n_pipes_per_port * sizeof(struct rte_sched_pipe);
489 uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue);
490 uint32_t size_queue_extra
491 = n_queues_per_port * sizeof(struct rte_sched_queue_extra);
492 uint32_t size_pipe_profiles
493 = params->n_max_pipe_profiles * sizeof(struct rte_sched_pipe_profile);
494 uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port);
495 uint32_t size_per_pipe_queue_array, size_queue_array;
499 size_per_pipe_queue_array = 0;
500 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
501 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
502 size_per_pipe_queue_array +=
503 params->qsize[i] * sizeof(struct rte_mbuf *);
505 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
506 params->qsize[i] * sizeof(struct rte_mbuf *);
508 size_queue_array = n_pipes_per_port * size_per_pipe_queue_array;
512 if (array == e_RTE_SCHED_PORT_ARRAY_SUBPORT)
514 base += RTE_CACHE_LINE_ROUNDUP(size_subport);
516 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE)
518 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
520 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE)
522 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
524 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA)
526 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
528 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES)
530 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
532 if (array == e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY)
534 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
536 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY)
538 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
544 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
545 enum rte_sched_subport_array array)
547 uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
548 uint32_t n_subport_pipe_queues =
549 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
551 uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
552 uint32_t size_queue =
553 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
554 uint32_t size_queue_extra
555 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
556 uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
557 sizeof(struct rte_sched_pipe_profile);
558 uint32_t size_bmp_array =
559 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
560 uint32_t size_per_pipe_queue_array, size_queue_array;
564 size_per_pipe_queue_array = 0;
565 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
566 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
567 size_per_pipe_queue_array +=
568 params->qsize[i] * sizeof(struct rte_mbuf *);
570 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
571 params->qsize[i] * sizeof(struct rte_mbuf *);
573 size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
577 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
579 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
581 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
583 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
585 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
587 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
589 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
591 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
593 if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
595 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
597 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
599 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
605 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
609 subport->qsize_add[0] = 0;
611 /* Strict prority traffic class */
612 for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
613 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
615 /* Best-effort traffic class */
616 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
617 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
618 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
619 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
620 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
621 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
622 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
623 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
624 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
626 subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
627 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
631 rte_sched_port_log_pipe_profile(struct rte_sched_subport *subport, uint32_t i)
633 struct rte_sched_pipe_profile *p = subport->pipe_profiles + i;
635 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
636 " Token bucket: period = %u, credits per period = %u, size = %u\n"
637 " Traffic classes: period = %u,\n"
638 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
639 " Best-effort traffic class oversubscription: weight = %hhu\n"
640 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
645 p->tb_credits_per_period,
648 /* Traffic classes */
650 p->tc_credits_per_period[0],
651 p->tc_credits_per_period[1],
652 p->tc_credits_per_period[2],
653 p->tc_credits_per_period[3],
654 p->tc_credits_per_period[4],
655 p->tc_credits_per_period[5],
656 p->tc_credits_per_period[6],
657 p->tc_credits_per_period[7],
658 p->tc_credits_per_period[8],
659 p->tc_credits_per_period[9],
660 p->tc_credits_per_period[10],
661 p->tc_credits_per_period[11],
662 p->tc_credits_per_period[12],
664 /* Best-effort traffic class oversubscription */
668 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
671 static inline uint64_t
672 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
674 uint64_t time = time_ms;
676 time = (time * rate) / 1000;
682 rte_sched_pipe_profile_convert(struct rte_sched_subport *subport,
683 struct rte_sched_pipe_params *src,
684 struct rte_sched_pipe_profile *dst,
687 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
688 uint32_t lcd1, lcd2, lcd;
692 if (src->tb_rate == rate) {
693 dst->tb_credits_per_period = 1;
696 double tb_rate = (double) src->tb_rate
698 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
700 rte_approx(tb_rate, d,
701 &dst->tb_credits_per_period, &dst->tb_period);
704 dst->tb_size = src->tb_size;
706 /* Traffic Classes */
707 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
710 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
711 if (subport->qsize[i])
712 dst->tc_credits_per_period[i]
713 = rte_sched_time_ms_to_bytes(src->tc_period,
716 dst->tc_ov_weight = src->tc_ov_weight;
719 wrr_cost[0] = src->wrr_weights[0];
720 wrr_cost[1] = src->wrr_weights[1];
721 wrr_cost[2] = src->wrr_weights[2];
722 wrr_cost[3] = src->wrr_weights[3];
724 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
725 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
726 lcd = rte_get_lcd(lcd1, lcd2);
728 wrr_cost[0] = lcd / wrr_cost[0];
729 wrr_cost[1] = lcd / wrr_cost[1];
730 wrr_cost[2] = lcd / wrr_cost[2];
731 wrr_cost[3] = lcd / wrr_cost[3];
733 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
734 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
735 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
736 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
740 rte_sched_subport_config_pipe_profile_table(struct rte_sched_subport *subport,
741 struct rte_sched_subport_params *params, uint32_t rate)
745 for (i = 0; i < subport->n_pipe_profiles; i++) {
746 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
747 struct rte_sched_pipe_profile *dst = subport->pipe_profiles + i;
749 rte_sched_pipe_profile_convert(subport, src, dst, rate);
750 rte_sched_port_log_pipe_profile(subport, i);
753 subport->pipe_tc_be_rate_max = 0;
754 for (i = 0; i < subport->n_pipe_profiles; i++) {
755 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
756 uint32_t pipe_tc_be_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
758 if (subport->pipe_tc_be_rate_max < pipe_tc_be_rate)
759 subport->pipe_tc_be_rate_max = pipe_tc_be_rate;
764 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
765 uint32_t n_max_pipes_per_subport,
770 /* Check user parameters */
771 if (params == NULL) {
773 "%s: Incorrect value for parameter params\n", __func__);
777 if (params->tb_rate == 0 || params->tb_rate > rate) {
779 "%s: Incorrect value for tb rate\n", __func__);
783 if (params->tb_size == 0) {
785 "%s: Incorrect value for tb size\n", __func__);
789 /* qsize: if non-zero, power of 2,
790 * no bigger than 32K (due to 16-bit read/write pointers)
792 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
793 uint16_t qsize = params->qsize[i];
795 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
797 "%s: Incorrect value for qsize\n", __func__);
802 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
803 uint32_t tc_rate = params->tc_rate[i];
804 uint16_t qsize = params->qsize[i];
806 if ((qsize == 0 && tc_rate != 0) ||
807 (qsize != 0 && tc_rate == 0) ||
808 (tc_rate > params->tb_rate)) {
810 "%s: Incorrect value for tc rate\n", __func__);
815 if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
816 params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
818 "%s: Incorrect qsize or tc rate(best effort)\n", __func__);
822 if (params->tc_period == 0) {
824 "%s: Incorrect value for tc period\n", __func__);
828 /* n_pipes_per_subport: non-zero, power of 2 */
829 if (params->n_pipes_per_subport_enabled == 0 ||
830 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
831 !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
833 "%s: Incorrect value for pipes number\n", __func__);
837 /* pipe_profiles and n_pipe_profiles */
838 if (params->pipe_profiles == NULL ||
839 params->n_pipe_profiles == 0 ||
840 params->n_max_pipe_profiles == 0 ||
841 params->n_pipe_profiles > params->n_max_pipe_profiles) {
843 "%s: Incorrect value for pipe profiles\n", __func__);
847 for (i = 0; i < params->n_pipe_profiles; i++) {
848 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
851 status = pipe_profile_check(p, rate, ¶ms->qsize[0]);
854 "%s: Pipe profile check failed(%d)\n", __func__, status);
863 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
865 uint32_t size0, size1;
868 status = rte_sched_port_check_params(params);
870 RTE_LOG(NOTICE, SCHED,
871 "Port scheduler params check failed (%d)\n", status);
876 size0 = sizeof(struct rte_sched_port);
877 size1 = rte_sched_port_get_array_base(params,
878 e_RTE_SCHED_PORT_ARRAY_TOTAL);
880 return size0 + size1;
883 struct rte_sched_port *
884 rte_sched_port_config(struct rte_sched_port_params *params)
886 struct rte_sched_port *port = NULL;
887 uint32_t size0, size1;
888 uint32_t cycles_per_byte;
892 status = rte_sched_port_check_params(params);
895 "%s: Port scheduler params check failed (%d)\n",
900 size0 = sizeof(struct rte_sched_port);
901 size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
903 /* Allocate memory to store the data structures */
904 port = rte_zmalloc_socket("qos_params", size0 + size1, RTE_CACHE_LINE_SIZE,
907 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
912 /* User parameters */
913 port->n_subports_per_port = params->n_subports_per_port;
914 port->n_pipes_per_subport = params->n_pipes_per_subport;
915 port->n_pipes_per_subport_log2 =
916 __builtin_ctz(params->n_pipes_per_subport);
917 port->socket = params->socket;
919 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
920 port->pipe_queue[i] = i;
922 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
923 port->pipe_tc[i] = j;
925 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
929 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
930 port->tc_queue[i] = j;
932 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
935 port->rate = params->rate;
936 port->mtu = params->mtu + params->frame_overhead;
937 port->frame_overhead = params->frame_overhead;
940 port->time_cpu_cycles = rte_get_tsc_cycles();
941 port->time_cpu_bytes = 0;
944 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
946 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
949 port->pkts_out = NULL;
950 port->n_pkts_out = 0;
956 rte_sched_subport_free(struct rte_sched_port *port,
957 struct rte_sched_subport *subport)
959 uint32_t n_subport_pipe_queues;
965 n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
967 /* Free enqueued mbufs */
968 for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
969 struct rte_mbuf **mbufs =
970 rte_sched_subport_pipe_qbase(subport, qindex);
971 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
973 struct rte_sched_queue *queue = subport->queue + qindex;
974 uint16_t qr = queue->qr & (qsize - 1);
975 uint16_t qw = queue->qw & (qsize - 1);
977 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
978 rte_pktmbuf_free(mbufs[qr]);
982 rte_bitmap_free(subport->bmp);
986 rte_sched_port_free(struct rte_sched_port *port)
990 /* Check user parameters */
994 for (i = 0; i < port->n_subports_per_port; i++)
995 rte_sched_subport_free(port, port->subports[i]);
1001 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
1003 struct rte_sched_subport *s = port->subports[i];
1005 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
1006 " Token bucket: period = %u, credits per period = %u, size = %u\n"
1007 " Traffic classes: period = %u\n"
1008 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
1009 " Best effort traffic class oversubscription: wm min = %u, wm max = %u\n",
1014 s->tb_credits_per_period,
1017 /* Traffic classes */
1019 s->tc_credits_per_period[0],
1020 s->tc_credits_per_period[1],
1021 s->tc_credits_per_period[2],
1022 s->tc_credits_per_period[3],
1023 s->tc_credits_per_period[4],
1024 s->tc_credits_per_period[5],
1025 s->tc_credits_per_period[6],
1026 s->tc_credits_per_period[7],
1027 s->tc_credits_per_period[8],
1028 s->tc_credits_per_period[9],
1029 s->tc_credits_per_period[10],
1030 s->tc_credits_per_period[11],
1031 s->tc_credits_per_period[12],
1033 /* Best effort traffic class oversubscription */
1039 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1043 for (i = 0; i < n_subports; i++) {
1044 struct rte_sched_subport *subport = port->subports[i];
1046 rte_sched_subport_free(port, subport);
1053 rte_sched_subport_config(struct rte_sched_port *port,
1054 uint32_t subport_id,
1055 struct rte_sched_subport_params *params)
1057 struct rte_sched_subport *s = NULL;
1058 uint32_t n_subports = subport_id;
1059 uint32_t n_subport_pipe_queues, i;
1060 uint32_t size0, size1, bmp_mem_size;
1063 /* Check user parameters */
1066 "%s: Incorrect value for parameter port\n", __func__);
1070 if (subport_id >= port->n_subports_per_port) {
1072 "%s: Incorrect value for subport id\n", __func__);
1074 rte_sched_free_memory(port, n_subports);
1078 status = rte_sched_subport_check_params(params,
1079 port->n_pipes_per_subport,
1082 RTE_LOG(NOTICE, SCHED,
1083 "%s: Port scheduler params check failed (%d)\n",
1086 rte_sched_free_memory(port, n_subports);
1090 /* Determine the amount of memory to allocate */
1091 size0 = sizeof(struct rte_sched_subport);
1092 size1 = rte_sched_subport_get_array_base(params,
1093 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1095 /* Allocate memory to store the data structures */
1096 s = rte_zmalloc_socket("subport_params", size0 + size1,
1097 RTE_CACHE_LINE_SIZE, port->socket);
1100 "%s: Memory allocation fails\n", __func__);
1102 rte_sched_free_memory(port, n_subports);
1109 port->subports[subport_id] = s;
1111 /* Token Bucket (TB) */
1112 if (params->tb_rate == port->rate) {
1113 s->tb_credits_per_period = 1;
1116 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
1117 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
1119 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
1122 s->tb_size = params->tb_size;
1123 s->tb_time = port->time;
1124 s->tb_credits = s->tb_size / 2;
1126 /* Traffic Classes (TCs) */
1127 s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
1128 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1129 if (params->qsize[i])
1130 s->tc_credits_per_period[i]
1131 = rte_sched_time_ms_to_bytes(params->tc_period,
1132 params->tc_rate[i]);
1134 s->tc_time = port->time + s->tc_period;
1135 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1136 if (params->qsize[i])
1137 s->tc_credits[i] = s->tc_credits_per_period[i];
1139 /* compile time checks */
1140 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1141 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1142 (RTE_SCHED_PORT_N_GRINDERS - 1));
1144 /* User parameters */
1145 s->n_pipes_per_subport_enabled = params->n_pipes_per_subport_enabled;
1146 memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1147 s->n_pipe_profiles = params->n_pipe_profiles;
1148 s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1150 #ifdef RTE_SCHED_RED
1151 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1154 for (j = 0; j < RTE_COLORS; j++) {
1155 /* if min/max are both zero, then RED is disabled */
1156 if ((params->red_params[i][j].min_th |
1157 params->red_params[i][j].max_th) == 0) {
1161 if (rte_red_config_init(&s->red_config[i][j],
1162 params->red_params[i][j].wq_log2,
1163 params->red_params[i][j].min_th,
1164 params->red_params[i][j].max_th,
1165 params->red_params[i][j].maxp_inv) != 0) {
1166 rte_sched_free_memory(port, n_subports);
1168 RTE_LOG(NOTICE, SCHED,
1169 "%s: RED configuration init fails\n", __func__);
1176 /* Scheduling loop detection */
1177 s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1178 s->pipe_exhaustion = 0;
1181 s->busy_grinders = 0;
1183 /* Queue base calculation */
1184 rte_sched_subport_config_qsize(s);
1186 /* Large data structures */
1187 s->pipe = (struct rte_sched_pipe *)
1188 (s->memory + rte_sched_subport_get_array_base(params,
1189 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1190 s->queue = (struct rte_sched_queue *)
1191 (s->memory + rte_sched_subport_get_array_base(params,
1192 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1193 s->queue_extra = (struct rte_sched_queue_extra *)
1194 (s->memory + rte_sched_subport_get_array_base(params,
1195 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1196 s->pipe_profiles = (struct rte_sched_pipe_profile *)
1197 (s->memory + rte_sched_subport_get_array_base(params,
1198 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1199 s->bmp_array = s->memory + rte_sched_subport_get_array_base(params,
1200 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1201 s->queue_array = (struct rte_mbuf **)
1202 (s->memory + rte_sched_subport_get_array_base(params,
1203 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1205 /* Pipe profile table */
1206 rte_sched_subport_config_pipe_profile_table(s, params, port->rate);
1209 n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1210 bmp_mem_size = rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
1211 s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1213 if (s->bmp == NULL) {
1215 "%s: Subport bitmap init error\n", __func__);
1217 rte_sched_free_memory(port, n_subports);
1221 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1222 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1224 #ifdef RTE_SCHED_SUBPORT_TC_OV
1225 /* TC oversubscription */
1226 s->tc_ov_wm_min = port->mtu;
1227 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
1228 s->pipe_tc_be_rate_max);
1229 s->tc_ov_wm = s->tc_ov_wm_max;
1230 s->tc_ov_period_id = 0;
1236 rte_sched_port_log_subport_config(port, subport_id);
1242 rte_sched_pipe_config(struct rte_sched_port *port,
1243 uint32_t subport_id,
1245 int32_t pipe_profile)
1247 struct rte_sched_subport *s;
1248 struct rte_sched_pipe *p;
1249 struct rte_sched_pipe_profile *params;
1250 uint32_t n_subports = subport_id + 1;
1251 uint32_t deactivate, profile, i;
1253 /* Check user parameters */
1254 profile = (uint32_t) pipe_profile;
1255 deactivate = (pipe_profile < 0);
1259 "%s: Incorrect value for parameter port\n", __func__);
1263 if (subport_id >= port->n_subports_per_port) {
1265 "%s: Incorrect value for parameter subport id\n", __func__);
1267 rte_sched_free_memory(port, n_subports);
1271 s = port->subports[subport_id];
1272 if (pipe_id >= s->n_pipes_per_subport_enabled) {
1274 "%s: Incorrect value for parameter pipe id\n", __func__);
1276 rte_sched_free_memory(port, n_subports);
1280 if (!deactivate && profile >= s->n_pipe_profiles) {
1282 "%s: Incorrect value for parameter pipe profile\n", __func__);
1284 rte_sched_free_memory(port, n_subports);
1288 /* Handle the case when pipe already has a valid configuration */
1289 p = s->pipe + pipe_id;
1291 params = s->pipe_profiles + p->profile;
1293 double subport_tc_be_rate =
1294 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1295 / (double) s->tc_period;
1296 double pipe_tc_be_rate =
1297 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1298 / (double) params->tc_period;
1299 uint32_t tc_be_ov = s->tc_ov;
1301 /* Unplug pipe from its subport */
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 OFF (%.4lf >= %.4lf)\n",
1309 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1312 /* Reset the pipe */
1313 memset(p, 0, sizeof(struct rte_sched_pipe));
1319 /* Apply the new pipe configuration */
1320 p->profile = profile;
1321 params = s->pipe_profiles + p->profile;
1323 /* Token Bucket (TB) */
1324 p->tb_time = port->time;
1325 p->tb_credits = params->tb_size / 2;
1327 /* Traffic Classes (TCs) */
1328 p->tc_time = port->time + params->tc_period;
1330 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1332 p->tc_credits[i] = params->tc_credits_per_period[i];
1335 /* Subport best effort tc oversubscription */
1336 double subport_tc_be_rate =
1337 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1338 / (double) s->tc_period;
1339 double pipe_tc_be_rate =
1340 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1341 / (double) params->tc_period;
1342 uint32_t tc_be_ov = s->tc_ov;
1344 s->tc_ov_n += params->tc_ov_weight;
1345 s->tc_ov_rate += pipe_tc_be_rate;
1346 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1348 if (s->tc_ov != tc_be_ov) {
1349 RTE_LOG(DEBUG, SCHED,
1350 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1351 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1353 p->tc_ov_period_id = s->tc_ov_period_id;
1354 p->tc_ov_credits = s->tc_ov_wm;
1361 rte_sched_subport_pipe_profile_add(struct rte_sched_port *port,
1362 uint32_t subport_id,
1363 struct rte_sched_pipe_params *params,
1364 uint32_t *pipe_profile_id)
1366 struct rte_sched_subport *s;
1367 struct rte_sched_pipe_profile *pp;
1374 "%s: Incorrect value for parameter port\n", __func__);
1378 /* Subport id not exceeds the max limit */
1379 if (subport_id > port->n_subports_per_port) {
1381 "%s: Incorrect value for subport id\n", __func__);
1385 s = port->subports[subport_id];
1387 /* Pipe profiles exceeds the max limit */
1388 if (s->n_pipe_profiles >= s->n_max_pipe_profiles) {
1390 "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1395 status = pipe_profile_check(params, port->rate, &s->qsize[0]);
1398 "%s: Pipe profile check failed(%d)\n", __func__, status);
1402 pp = &s->pipe_profiles[s->n_pipe_profiles];
1403 rte_sched_pipe_profile_convert(s, params, pp, port->rate);
1405 /* Pipe profile should not exists */
1406 for (i = 0; i < s->n_pipe_profiles; i++)
1407 if (memcmp(s->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1409 "%s: Pipe profile exists\n", __func__);
1413 /* Pipe profile commit */
1414 *pipe_profile_id = s->n_pipe_profiles;
1415 s->n_pipe_profiles++;
1417 if (s->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1418 s->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1420 rte_sched_port_log_pipe_profile(s, *pipe_profile_id);
1425 static inline uint32_t
1426 rte_sched_port_qindex(struct rte_sched_port *port,
1429 uint32_t traffic_class,
1432 return ((subport & (port->n_subports_per_port - 1)) <<
1433 (port->n_pipes_per_subport_log2 + 4)) |
1435 (port->subports[subport]->n_pipes_per_subport_enabled - 1)) << 4) |
1436 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1437 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1441 rte_sched_port_pkt_write(struct rte_sched_port *port,
1442 struct rte_mbuf *pkt,
1443 uint32_t subport, uint32_t pipe,
1444 uint32_t traffic_class,
1445 uint32_t queue, enum rte_color color)
1448 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1450 rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1454 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1455 const struct rte_mbuf *pkt,
1456 uint32_t *subport, uint32_t *pipe,
1457 uint32_t *traffic_class, uint32_t *queue)
1459 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1461 *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1462 *pipe = (queue_id >> 4) &
1463 (port->subports[*subport]->n_pipes_per_subport_enabled - 1);
1464 *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1465 *queue = rte_sched_port_tc_queue(port, queue_id);
1469 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1471 return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1475 rte_sched_subport_read_stats(struct rte_sched_port *port,
1476 uint32_t subport_id,
1477 struct rte_sched_subport_stats *stats,
1480 struct rte_sched_subport *s;
1482 /* Check user parameters */
1485 "%s: Incorrect value for parameter port\n", __func__);
1489 if (subport_id >= port->n_subports_per_port) {
1491 "%s: Incorrect value for subport id\n", __func__);
1495 if (stats == NULL) {
1497 "%s: Incorrect value for parameter stats\n", __func__);
1501 if (tc_ov == NULL) {
1503 "%s: Incorrect value for tc_ov\n", __func__);
1507 s = port->subports[subport_id];
1509 /* Copy subport stats and clear */
1510 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1511 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1513 /* Subport TC oversubscription status */
1520 rte_sched_queue_read_stats(struct rte_sched_port *port,
1522 struct rte_sched_queue_stats *stats,
1525 struct rte_sched_queue *q;
1526 struct rte_sched_queue_extra *qe;
1528 /* Check user parameters */
1531 "%s: Incorrect value for parameter port\n", __func__);
1535 if (queue_id >= rte_sched_port_queues_per_port(port)) {
1537 "%s: Incorrect value for queue id\n", __func__);
1541 if (stats == NULL) {
1543 "%s: Incorrect value for parameter stats\n", __func__);
1549 "%s: Incorrect value for parameter qlen\n", __func__);
1552 q = port->queue + queue_id;
1553 qe = port->queue_extra + queue_id;
1555 /* Copy queue stats and clear */
1556 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1557 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1560 *qlen = q->qw - q->qr;
1565 #ifdef RTE_SCHED_DEBUG
1568 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1570 struct rte_sched_queue *queue = port->queue + qindex;
1572 return queue->qr == queue->qw;
1575 #endif /* RTE_SCHED_DEBUG */
1577 #ifdef RTE_SCHED_COLLECT_STATS
1580 rte_sched_port_update_subport_stats(struct rte_sched_port *port,
1581 struct rte_sched_subport *subport,
1583 struct rte_mbuf *pkt)
1585 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1586 uint32_t pkt_len = pkt->pkt_len;
1588 subport->stats.n_pkts_tc[tc_index] += 1;
1589 subport->stats.n_bytes_tc[tc_index] += pkt_len;
1592 #ifdef RTE_SCHED_RED
1594 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1595 struct rte_sched_subport *subport,
1597 struct rte_mbuf *pkt,
1601 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1602 struct rte_sched_subport *subport,
1604 struct rte_mbuf *pkt,
1605 __rte_unused uint32_t red)
1608 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1609 uint32_t pkt_len = pkt->pkt_len;
1611 subport->stats.n_pkts_tc_dropped[tc_index] += 1;
1612 subport->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1613 #ifdef RTE_SCHED_RED
1614 subport->stats.n_pkts_red_dropped[tc_index] += red;
1619 rte_sched_port_update_queue_stats(struct rte_sched_subport *subport,
1621 struct rte_mbuf *pkt)
1623 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1624 uint32_t pkt_len = pkt->pkt_len;
1626 qe->stats.n_pkts += 1;
1627 qe->stats.n_bytes += pkt_len;
1630 #ifdef RTE_SCHED_RED
1632 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1634 struct rte_mbuf *pkt,
1638 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1640 struct rte_mbuf *pkt,
1641 __rte_unused uint32_t red)
1644 struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1645 uint32_t pkt_len = pkt->pkt_len;
1647 qe->stats.n_pkts_dropped += 1;
1648 qe->stats.n_bytes_dropped += pkt_len;
1649 #ifdef RTE_SCHED_RED
1650 qe->stats.n_pkts_red_dropped += red;
1654 #endif /* RTE_SCHED_COLLECT_STATS */
1656 #ifdef RTE_SCHED_RED
1659 rte_sched_port_red_drop(struct rte_sched_port *port,
1660 struct rte_sched_subport *subport,
1661 struct rte_mbuf *pkt,
1665 struct rte_sched_queue_extra *qe;
1666 struct rte_red_config *red_cfg;
1667 struct rte_red *red;
1669 enum rte_color color;
1671 tc_index = rte_sched_port_pipe_tc(port, qindex);
1672 color = rte_sched_port_pkt_read_color(pkt);
1673 red_cfg = &subport->red_config[tc_index][color];
1675 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1678 qe = subport->queue_extra + qindex;
1681 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1685 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1687 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1688 struct rte_red *red = &qe->red;
1690 rte_red_mark_queue_empty(red, port->time);
1695 static inline int rte_sched_port_red_drop(struct rte_sched_port *port __rte_unused,
1696 struct rte_sched_subport *subport __rte_unused,
1697 struct rte_mbuf *pkt __rte_unused,
1698 uint32_t qindex __rte_unused,
1699 uint16_t qlen __rte_unused)
1704 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1706 #endif /* RTE_SCHED_RED */
1708 #ifdef RTE_SCHED_DEBUG
1711 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1718 rte_panic("Empty slab at position %u\n", bmp_pos);
1721 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1722 if (mask & bmp_slab) {
1723 if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1724 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1731 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1735 #endif /* RTE_SCHED_DEBUG */
1737 static inline struct rte_sched_subport *
1738 rte_sched_port_subport(struct rte_sched_port *port,
1739 struct rte_mbuf *pkt)
1741 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1742 uint32_t subport_id = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1744 return port->subports[subport_id];
1747 static inline uint32_t
1748 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_subport *subport,
1749 struct rte_mbuf *pkt, uint32_t subport_qmask)
1751 struct rte_sched_queue *q;
1752 #ifdef RTE_SCHED_COLLECT_STATS
1753 struct rte_sched_queue_extra *qe;
1755 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1756 uint32_t subport_queue_id = subport_qmask & qindex;
1758 q = subport->queue + subport_queue_id;
1760 #ifdef RTE_SCHED_COLLECT_STATS
1761 qe = subport->queue_extra + subport_queue_id;
1765 return subport_queue_id;
1769 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1770 struct rte_sched_subport *subport,
1772 struct rte_mbuf **qbase)
1774 struct rte_sched_queue *q;
1775 struct rte_mbuf **q_qw;
1778 q = subport->queue + qindex;
1779 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1780 q_qw = qbase + (q->qw & (qsize - 1));
1782 rte_prefetch0(q_qw);
1783 rte_bitmap_prefetch0(subport->bmp, qindex);
1787 rte_sched_port_enqueue_qwa(struct rte_sched_port *port,
1788 struct rte_sched_subport *subport,
1790 struct rte_mbuf **qbase,
1791 struct rte_mbuf *pkt)
1793 struct rte_sched_queue *q;
1797 q = subport->queue + qindex;
1798 qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1799 qlen = q->qw - q->qr;
1801 /* Drop the packet (and update drop stats) when queue is full */
1802 if (unlikely(rte_sched_port_red_drop(port, subport, pkt, qindex, qlen) ||
1804 rte_pktmbuf_free(pkt);
1805 #ifdef RTE_SCHED_COLLECT_STATS
1806 rte_sched_port_update_subport_stats_on_drop(port, subport,
1807 qindex, pkt, qlen < qsize);
1808 rte_sched_port_update_queue_stats_on_drop(subport, qindex, pkt,
1814 /* Enqueue packet */
1815 qbase[q->qw & (qsize - 1)] = pkt;
1818 /* Activate queue in the subport bitmap */
1819 rte_bitmap_set(subport->bmp, qindex);
1822 #ifdef RTE_SCHED_COLLECT_STATS
1823 rte_sched_port_update_subport_stats(port, subport, qindex, pkt);
1824 rte_sched_port_update_queue_stats(subport, qindex, pkt);
1832 * The enqueue function implements a 4-level pipeline with each stage
1833 * processing two different packets. The purpose of using a pipeline
1834 * is to hide the latency of prefetching the data structures. The
1835 * naming convention is presented in the diagram below:
1837 * p00 _______ p10 _______ p20 _______ p30 _______
1838 * ----->| |----->| |----->| |----->| |----->
1839 * | 0 | | 1 | | 2 | | 3 |
1840 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1845 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1848 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1849 *pkt30, *pkt31, *pkt_last;
1850 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1851 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1852 struct rte_sched_subport *subport00, *subport01, *subport10, *subport11,
1853 *subport20, *subport21, *subport30, *subport31, *subport_last;
1854 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1855 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1856 uint32_t subport_qmask;
1860 subport_qmask = (1 << (port->n_pipes_per_subport_log2 + 4)) - 1;
1863 * Less then 6 input packets available, which is not enough to
1866 if (unlikely(n_pkts < 6)) {
1867 struct rte_sched_subport *subports[5];
1868 struct rte_mbuf **q_base[5];
1871 /* Prefetch the mbuf structure of each packet */
1872 for (i = 0; i < n_pkts; i++)
1873 rte_prefetch0(pkts[i]);
1875 /* Prefetch the subport structure for each packet */
1876 for (i = 0; i < n_pkts; i++)
1877 subports[i] = rte_sched_port_subport(port, pkts[i]);
1879 /* Prefetch the queue structure for each queue */
1880 for (i = 0; i < n_pkts; i++)
1881 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(subports[i],
1882 pkts[i], subport_qmask);
1884 /* Prefetch the write pointer location of each queue */
1885 for (i = 0; i < n_pkts; i++) {
1886 q_base[i] = rte_sched_subport_pipe_qbase(subports[i], q[i]);
1887 rte_sched_port_enqueue_qwa_prefetch0(port, subports[i],
1891 /* Write each packet to its queue */
1892 for (i = 0; i < n_pkts; i++)
1893 result += rte_sched_port_enqueue_qwa(port, subports[i],
1894 q[i], q_base[i], pkts[i]);
1899 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1902 rte_prefetch0(pkt20);
1903 rte_prefetch0(pkt21);
1907 rte_prefetch0(pkt10);
1908 rte_prefetch0(pkt11);
1910 subport20 = rte_sched_port_subport(port, pkt20);
1911 subport21 = rte_sched_port_subport(port, pkt21);
1912 q20 = rte_sched_port_enqueue_qptrs_prefetch0(subport20,
1913 pkt20, subport_qmask);
1914 q21 = rte_sched_port_enqueue_qptrs_prefetch0(subport21,
1915 pkt21, subport_qmask);
1919 rte_prefetch0(pkt00);
1920 rte_prefetch0(pkt01);
1922 subport10 = rte_sched_port_subport(port, pkt10);
1923 subport11 = rte_sched_port_subport(port, pkt11);
1924 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
1925 pkt10, subport_qmask);
1926 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
1927 pkt11, subport_qmask);
1929 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
1930 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
1931 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
1932 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
1934 /* Run the pipeline */
1935 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1936 /* Propagate stage inputs */
1947 subport30 = subport20;
1948 subport31 = subport21;
1949 subport20 = subport10;
1950 subport21 = subport11;
1951 q30_base = q20_base;
1952 q31_base = q21_base;
1954 /* Stage 0: Get packets in */
1956 pkt01 = pkts[i + 1];
1957 rte_prefetch0(pkt00);
1958 rte_prefetch0(pkt01);
1960 /* Stage 1: Prefetch subport and queue structure storing queue pointers */
1961 subport10 = rte_sched_port_subport(port, pkt10);
1962 subport11 = rte_sched_port_subport(port, pkt11);
1963 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
1964 pkt10, subport_qmask);
1965 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
1966 pkt11, subport_qmask);
1968 /* Stage 2: Prefetch queue write location */
1969 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
1970 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
1971 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
1972 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
1974 /* Stage 3: Write packet to queue and activate queue */
1975 r30 = rte_sched_port_enqueue_qwa(port, subport30,
1976 q30, q30_base, pkt30);
1977 r31 = rte_sched_port_enqueue_qwa(port, subport31,
1978 q31, q31_base, pkt31);
1979 result += r30 + r31;
1983 * Drain the pipeline (exactly 6 packets).
1984 * Handle the last packet in the case
1985 * of an odd number of input packets.
1987 pkt_last = pkts[n_pkts - 1];
1988 rte_prefetch0(pkt_last);
1990 subport00 = rte_sched_port_subport(port, pkt00);
1991 subport01 = rte_sched_port_subport(port, pkt01);
1992 q00 = rte_sched_port_enqueue_qptrs_prefetch0(subport00,
1993 pkt00, subport_qmask);
1994 q01 = rte_sched_port_enqueue_qptrs_prefetch0(subport01,
1995 pkt01, subport_qmask);
1997 q10_base = rte_sched_subport_pipe_qbase(subport10, q10);
1998 q11_base = rte_sched_subport_pipe_qbase(subport11, q11);
1999 rte_sched_port_enqueue_qwa_prefetch0(port, subport10, q10, q10_base);
2000 rte_sched_port_enqueue_qwa_prefetch0(port, subport11, q11, q11_base);
2002 r20 = rte_sched_port_enqueue_qwa(port, subport20,
2003 q20, q20_base, pkt20);
2004 r21 = rte_sched_port_enqueue_qwa(port, subport21,
2005 q21, q21_base, pkt21);
2006 result += r20 + r21;
2008 subport_last = rte_sched_port_subport(port, pkt_last);
2009 q_last = rte_sched_port_enqueue_qptrs_prefetch0(subport_last,
2010 pkt_last, subport_qmask);
2012 q00_base = rte_sched_subport_pipe_qbase(subport00, q00);
2013 q01_base = rte_sched_subport_pipe_qbase(subport01, q01);
2014 rte_sched_port_enqueue_qwa_prefetch0(port, subport00, q00, q00_base);
2015 rte_sched_port_enqueue_qwa_prefetch0(port, subport01, q01, q01_base);
2017 r10 = rte_sched_port_enqueue_qwa(port, subport10, q10,
2019 r11 = rte_sched_port_enqueue_qwa(port, subport11, q11,
2021 result += r10 + r11;
2023 q_last_base = rte_sched_subport_pipe_qbase(subport_last, q_last);
2024 rte_sched_port_enqueue_qwa_prefetch0(port, subport_last,
2025 q_last, q_last_base);
2027 r00 = rte_sched_port_enqueue_qwa(port, subport00, q00,
2029 r01 = rte_sched_port_enqueue_qwa(port, subport01, q01,
2031 result += r00 + r01;
2034 r_last = rte_sched_port_enqueue_qwa(port, subport_last,
2035 q_last, q_last_base, pkt_last);
2042 #ifndef RTE_SCHED_SUBPORT_TC_OV
2045 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
2047 struct rte_sched_grinder *grinder = port->grinder + pos;
2048 struct rte_sched_subport *subport = grinder->subport;
2049 struct rte_sched_pipe *pipe = grinder->pipe;
2050 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2055 n_periods = (port->time - subport->tb_time) / subport->tb_period;
2056 subport->tb_credits += n_periods * subport->tb_credits_per_period;
2057 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
2058 subport->tb_time += n_periods * subport->tb_period;
2061 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2062 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2063 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
2064 pipe->tb_time += n_periods * params->tb_period;
2067 if (unlikely(port->time >= subport->tc_time)) {
2068 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2069 subport->tc_credits[i] = subport->tc_credits_per_period[i];
2071 subport->tc_time = port->time + subport->tc_period;
2075 if (unlikely(port->time >= pipe->tc_time)) {
2076 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2077 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2079 pipe->tc_time = port->time + params->tc_period;
2085 static inline uint32_t
2086 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
2088 struct rte_sched_grinder *grinder = port->grinder + pos;
2089 struct rte_sched_subport *subport = grinder->subport;
2090 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2091 uint32_t tc_consumption = 0, tc_ov_consumption_max;
2092 uint32_t tc_ov_wm = subport->tc_ov_wm;
2095 if (subport->tc_ov == 0)
2096 return subport->tc_ov_wm_max;
2098 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2099 tc_ov_consumption[i] =
2100 subport->tc_credits_per_period[i] - subport->tc_credits[i];
2101 tc_consumption += tc_ov_consumption[i];
2104 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
2105 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2106 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2108 tc_ov_consumption_max =
2109 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2112 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2113 (tc_ov_consumption_max - port->mtu)) {
2114 tc_ov_wm -= tc_ov_wm >> 7;
2115 if (tc_ov_wm < subport->tc_ov_wm_min)
2116 tc_ov_wm = subport->tc_ov_wm_min;
2121 tc_ov_wm += (tc_ov_wm >> 7) + 1;
2122 if (tc_ov_wm > subport->tc_ov_wm_max)
2123 tc_ov_wm = subport->tc_ov_wm_max;
2129 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
2131 struct rte_sched_grinder *grinder = port->grinder + pos;
2132 struct rte_sched_subport *subport = grinder->subport;
2133 struct rte_sched_pipe *pipe = grinder->pipe;
2134 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2139 n_periods = (port->time - subport->tb_time) / subport->tb_period;
2140 subport->tb_credits += n_periods * subport->tb_credits_per_period;
2141 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
2142 subport->tb_time += n_periods * subport->tb_period;
2145 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2146 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2147 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
2148 pipe->tb_time += n_periods * params->tb_period;
2151 if (unlikely(port->time >= subport->tc_time)) {
2152 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
2154 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2155 subport->tc_credits[i] = subport->tc_credits_per_period[i];
2157 subport->tc_time = port->time + subport->tc_period;
2158 subport->tc_ov_period_id++;
2162 if (unlikely(port->time >= pipe->tc_time)) {
2163 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2164 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2165 pipe->tc_time = port->time + params->tc_period;
2168 /* Pipe TCs - Oversubscription */
2169 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2170 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2172 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2176 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
2179 #ifndef RTE_SCHED_SUBPORT_TC_OV
2182 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
2184 struct rte_sched_grinder *grinder = port->grinder + pos;
2185 struct rte_sched_subport *subport = grinder->subport;
2186 struct rte_sched_pipe *pipe = grinder->pipe;
2187 struct rte_mbuf *pkt = grinder->pkt;
2188 uint32_t tc_index = grinder->tc_index;
2189 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2190 uint32_t subport_tb_credits = subport->tb_credits;
2191 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2192 uint32_t pipe_tb_credits = pipe->tb_credits;
2193 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2196 /* Check queue credits */
2197 enough_credits = (pkt_len <= subport_tb_credits) &&
2198 (pkt_len <= subport_tc_credits) &&
2199 (pkt_len <= pipe_tb_credits) &&
2200 (pkt_len <= pipe_tc_credits);
2202 if (!enough_credits)
2205 /* Update port credits */
2206 subport->tb_credits -= pkt_len;
2207 subport->tc_credits[tc_index] -= pkt_len;
2208 pipe->tb_credits -= pkt_len;
2209 pipe->tc_credits[tc_index] -= pkt_len;
2217 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
2219 struct rte_sched_grinder *grinder = port->grinder + pos;
2220 struct rte_sched_subport *subport = grinder->subport;
2221 struct rte_sched_pipe *pipe = grinder->pipe;
2222 struct rte_mbuf *pkt = grinder->pkt;
2223 uint32_t tc_index = grinder->tc_index;
2224 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2225 uint32_t subport_tb_credits = subport->tb_credits;
2226 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2227 uint32_t pipe_tb_credits = pipe->tb_credits;
2228 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2229 uint32_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2230 uint32_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2231 uint32_t pipe_tc_ov_credits, i;
2234 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2235 pipe_tc_ov_mask1[i] = UINT32_MAX;
2237 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2238 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = UINT32_MAX;
2239 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2241 /* Check pipe and subport credits */
2242 enough_credits = (pkt_len <= subport_tb_credits) &&
2243 (pkt_len <= subport_tc_credits) &&
2244 (pkt_len <= pipe_tb_credits) &&
2245 (pkt_len <= pipe_tc_credits) &&
2246 (pkt_len <= pipe_tc_ov_credits);
2248 if (!enough_credits)
2251 /* Update pipe and subport credits */
2252 subport->tb_credits -= pkt_len;
2253 subport->tc_credits[tc_index] -= pkt_len;
2254 pipe->tb_credits -= pkt_len;
2255 pipe->tc_credits[tc_index] -= pkt_len;
2256 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2261 #endif /* RTE_SCHED_SUBPORT_TC_OV */
2265 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
2267 struct rte_sched_grinder *grinder = port->grinder + pos;
2268 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2269 struct rte_mbuf *pkt = grinder->pkt;
2270 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2271 uint32_t be_tc_active;
2273 if (!grinder_credits_check(port, pos))
2276 /* Advance port time */
2277 port->time += pkt_len;
2280 port->pkts_out[port->n_pkts_out++] = pkt;
2283 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2284 grinder->wrr_tokens[grinder->qpos] +=
2285 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2287 if (queue->qr == queue->qw) {
2288 uint32_t qindex = grinder->qindex[grinder->qpos];
2290 rte_bitmap_clear(port->bmp, qindex);
2291 grinder->qmask &= ~(1 << grinder->qpos);
2293 grinder->wrr_mask[grinder->qpos] = 0;
2294 rte_sched_port_set_queue_empty_timestamp(port, qindex);
2297 /* Reset pipe loop detection */
2298 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2299 grinder->productive = 1;
2304 #ifdef SCHED_VECTOR_SSE4
2307 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2309 __m128i index = _mm_set1_epi32(base_pipe);
2310 __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
2311 __m128i res = _mm_cmpeq_epi32(pipes, index);
2313 pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
2314 pipes = _mm_cmpeq_epi32(pipes, index);
2315 res = _mm_or_si128(res, pipes);
2317 if (_mm_testz_si128(res, res))
2323 #elif defined(SCHED_VECTOR_NEON)
2326 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2328 uint32x4_t index, pipes;
2329 uint32_t *pos = (uint32_t *)port->grinder_base_bmp_pos;
2331 index = vmovq_n_u32(base_pipe);
2332 pipes = vld1q_u32(pos);
2333 if (!vminvq_u32(veorq_u32(pipes, index)))
2336 pipes = vld1q_u32(pos + 4);
2337 if (!vminvq_u32(veorq_u32(pipes, index)))
2346 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2350 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2351 if (port->grinder_base_bmp_pos[i] == base_pipe)
2358 #endif /* RTE_SCHED_OPTIMIZATIONS */
2361 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2363 struct rte_sched_grinder *grinder = port->grinder + pos;
2366 grinder->pcache_w = 0;
2367 grinder->pcache_r = 0;
2369 w[0] = (uint16_t) bmp_slab;
2370 w[1] = (uint16_t) (bmp_slab >> 16);
2371 w[2] = (uint16_t) (bmp_slab >> 32);
2372 w[3] = (uint16_t) (bmp_slab >> 48);
2374 grinder->pcache_qmask[grinder->pcache_w] = w[0];
2375 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2376 grinder->pcache_w += (w[0] != 0);
2378 grinder->pcache_qmask[grinder->pcache_w] = w[1];
2379 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2380 grinder->pcache_w += (w[1] != 0);
2382 grinder->pcache_qmask[grinder->pcache_w] = w[2];
2383 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2384 grinder->pcache_w += (w[2] != 0);
2386 grinder->pcache_qmask[grinder->pcache_w] = w[3];
2387 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2388 grinder->pcache_w += (w[3] != 0);
2392 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
2394 struct rte_sched_grinder *grinder = port->grinder + pos;
2397 grinder->tccache_w = 0;
2398 grinder->tccache_r = 0;
2400 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2401 b = (uint8_t) ((qmask >> i) & 0x1);
2402 grinder->tccache_qmask[grinder->tccache_w] = b;
2403 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2404 grinder->tccache_w += (b != 0);
2407 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2408 grinder->tccache_qmask[grinder->tccache_w] = b;
2409 grinder->tccache_qindex[grinder->tccache_w] = qindex +
2410 RTE_SCHED_TRAFFIC_CLASS_BE;
2411 grinder->tccache_w += (b != 0);
2415 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
2417 struct rte_sched_grinder *grinder = port->grinder + pos;
2418 struct rte_mbuf **qbase;
2422 if (grinder->tccache_r == grinder->tccache_w)
2425 qindex = grinder->tccache_qindex[grinder->tccache_r];
2426 qbase = rte_sched_port_qbase(port, qindex);
2427 qsize = rte_sched_port_qsize(port, qindex);
2429 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2430 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2431 grinder->qsize = qsize;
2433 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2434 grinder->queue[0] = port->queue + qindex;
2435 grinder->qbase[0] = qbase;
2436 grinder->qindex[0] = qindex;
2437 grinder->tccache_r++;
2442 grinder->queue[0] = port->queue + qindex;
2443 grinder->queue[1] = port->queue + qindex + 1;
2444 grinder->queue[2] = port->queue + qindex + 2;
2445 grinder->queue[3] = port->queue + qindex + 3;
2447 grinder->qbase[0] = qbase;
2448 grinder->qbase[1] = qbase + qsize;
2449 grinder->qbase[2] = qbase + 2 * qsize;
2450 grinder->qbase[3] = qbase + 3 * qsize;
2452 grinder->qindex[0] = qindex;
2453 grinder->qindex[1] = qindex + 1;
2454 grinder->qindex[2] = qindex + 2;
2455 grinder->qindex[3] = qindex + 3;
2457 grinder->tccache_r++;
2462 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
2464 struct rte_sched_grinder *grinder = port->grinder + pos;
2465 uint32_t pipe_qindex;
2466 uint16_t pipe_qmask;
2468 if (grinder->pcache_r < grinder->pcache_w) {
2469 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2470 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2471 grinder->pcache_r++;
2473 uint64_t bmp_slab = 0;
2474 uint32_t bmp_pos = 0;
2476 /* Get another non-empty pipe group */
2477 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
2480 #ifdef RTE_SCHED_DEBUG
2481 debug_check_queue_slab(port, bmp_pos, bmp_slab);
2484 /* Return if pipe group already in one of the other grinders */
2485 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2486 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
2489 port->grinder_base_bmp_pos[pos] = bmp_pos;
2491 /* Install new pipe group into grinder's pipe cache */
2492 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
2494 pipe_qmask = grinder->pcache_qmask[0];
2495 pipe_qindex = grinder->pcache_qindex[0];
2496 grinder->pcache_r = 1;
2499 /* Install new pipe in the grinder */
2500 grinder->pindex = pipe_qindex >> 4;
2501 grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
2502 grinder->pipe = port->pipe + grinder->pindex;
2503 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2504 grinder->productive = 0;
2506 grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
2507 grinder_next_tc(port, pos);
2509 /* Check for pipe exhaustion */
2510 if (grinder->pindex == port->pipe_loop) {
2511 port->pipe_exhaustion = 1;
2512 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2520 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
2522 struct rte_sched_grinder *grinder = port->grinder + pos;
2523 struct rte_sched_pipe *pipe = grinder->pipe;
2524 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2525 uint32_t qmask = grinder->qmask;
2527 grinder->wrr_tokens[0] =
2528 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2529 grinder->wrr_tokens[1] =
2530 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2531 grinder->wrr_tokens[2] =
2532 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2533 grinder->wrr_tokens[3] =
2534 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2536 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2537 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2538 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2539 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2541 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2542 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2543 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2544 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2548 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
2550 struct rte_sched_grinder *grinder = port->grinder + pos;
2551 struct rte_sched_pipe *pipe = grinder->pipe;
2553 pipe->wrr_tokens[0] =
2554 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2555 RTE_SCHED_WRR_SHIFT;
2556 pipe->wrr_tokens[1] =
2557 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2558 RTE_SCHED_WRR_SHIFT;
2559 pipe->wrr_tokens[2] =
2560 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2561 RTE_SCHED_WRR_SHIFT;
2562 pipe->wrr_tokens[3] =
2563 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2564 RTE_SCHED_WRR_SHIFT;
2568 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
2570 struct rte_sched_grinder *grinder = port->grinder + pos;
2571 uint16_t wrr_tokens_min;
2573 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2574 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2575 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2576 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2578 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2579 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2581 grinder->wrr_tokens[0] -= wrr_tokens_min;
2582 grinder->wrr_tokens[1] -= wrr_tokens_min;
2583 grinder->wrr_tokens[2] -= wrr_tokens_min;
2584 grinder->wrr_tokens[3] -= wrr_tokens_min;
2588 #define grinder_evict(port, pos)
2591 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
2593 struct rte_sched_grinder *grinder = port->grinder + pos;
2595 rte_prefetch0(grinder->pipe);
2596 rte_prefetch0(grinder->queue[0]);
2600 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
2602 struct rte_sched_grinder *grinder = port->grinder + pos;
2603 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2605 qsize = grinder->qsize;
2608 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2609 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2611 rte_prefetch0(grinder->qbase[0] + qr[0]);
2615 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2616 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2617 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2618 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2620 rte_prefetch0(grinder->qbase[0] + qr[0]);
2621 rte_prefetch0(grinder->qbase[1] + qr[1]);
2623 grinder_wrr_load(port, pos);
2624 grinder_wrr(port, pos);
2626 rte_prefetch0(grinder->qbase[2] + qr[2]);
2627 rte_prefetch0(grinder->qbase[3] + qr[3]);
2631 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2633 struct rte_sched_grinder *grinder = port->grinder + pos;
2634 uint32_t qpos = grinder->qpos;
2635 struct rte_mbuf **qbase = grinder->qbase[qpos];
2636 uint16_t qsize = grinder->qsize;
2637 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2639 grinder->pkt = qbase[qr];
2640 rte_prefetch0(grinder->pkt);
2642 if (unlikely((qr & 0x7) == 7)) {
2643 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2645 rte_prefetch0(qbase + qr_next);
2649 static inline uint32_t
2650 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2652 struct rte_sched_grinder *grinder = port->grinder + pos;
2654 switch (grinder->state) {
2655 case e_GRINDER_PREFETCH_PIPE:
2657 if (grinder_next_pipe(port, pos)) {
2658 grinder_prefetch_pipe(port, pos);
2659 port->busy_grinders++;
2661 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2668 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2670 struct rte_sched_pipe *pipe = grinder->pipe;
2672 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2673 grinder_prefetch_tc_queue_arrays(port, pos);
2674 grinder_credits_update(port, pos);
2676 grinder->state = e_GRINDER_PREFETCH_MBUF;
2680 case e_GRINDER_PREFETCH_MBUF:
2682 grinder_prefetch_mbuf(port, pos);
2684 grinder->state = e_GRINDER_READ_MBUF;
2688 case e_GRINDER_READ_MBUF:
2690 uint32_t wrr_active, result = 0;
2692 result = grinder_schedule(port, pos);
2694 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2696 /* Look for next packet within the same TC */
2697 if (result && grinder->qmask) {
2699 grinder_wrr(port, pos);
2701 grinder_prefetch_mbuf(port, pos);
2707 grinder_wrr_store(port, pos);
2709 /* Look for another active TC within same pipe */
2710 if (grinder_next_tc(port, pos)) {
2711 grinder_prefetch_tc_queue_arrays(port, pos);
2713 grinder->state = e_GRINDER_PREFETCH_MBUF;
2717 if (grinder->productive == 0 &&
2718 port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2719 port->pipe_loop = grinder->pindex;
2721 grinder_evict(port, pos);
2723 /* Look for another active pipe */
2724 if (grinder_next_pipe(port, pos)) {
2725 grinder_prefetch_pipe(port, pos);
2727 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2731 /* No active pipe found */
2732 port->busy_grinders--;
2734 grinder->state = e_GRINDER_PREFETCH_PIPE;
2739 rte_panic("Algorithmic error (invalid state)\n");
2745 rte_sched_port_time_resync(struct rte_sched_port *port)
2747 uint64_t cycles = rte_get_tsc_cycles();
2748 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2749 uint64_t bytes_diff;
2751 /* Compute elapsed time in bytes */
2752 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2753 port->inv_cycles_per_byte);
2755 /* Advance port time */
2756 port->time_cpu_cycles = cycles;
2757 port->time_cpu_bytes += bytes_diff;
2758 if (port->time < port->time_cpu_bytes)
2759 port->time = port->time_cpu_bytes;
2761 /* Reset pipe loop detection */
2762 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2766 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2770 /* Check if any exception flag is set */
2771 exceptions = (second_pass && port->busy_grinders == 0) ||
2772 (port->pipe_exhaustion == 1);
2774 /* Clear exception flags */
2775 port->pipe_exhaustion = 0;
2781 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2785 port->pkts_out = pkts;
2786 port->n_pkts_out = 0;
2788 rte_sched_port_time_resync(port);
2790 /* Take each queue in the grinder one step further */
2791 for (i = 0, count = 0; ; i++) {
2792 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2793 if ((count == n_pkts) ||
2794 rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {