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 #ifdef RTE_SCHED_COLLECT_STATS
286 static inline uint32_t
287 rte_sched_port_queues_per_subport(struct rte_sched_port *port)
289 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport;
294 static inline uint32_t
295 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
297 return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
300 static inline struct rte_mbuf **
301 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
303 uint32_t pindex = qindex >> 4;
304 uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
306 return (subport->queue_array + pindex *
307 subport->qsize_sum + subport->qsize_add[qpos]);
310 static inline uint16_t
311 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
312 struct rte_sched_subport *subport, uint32_t qindex)
314 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
316 return subport->qsize[tc];
319 static inline uint32_t
320 rte_sched_port_queues_per_port(struct rte_sched_port *port)
322 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport * port->n_subports_per_port;
325 static inline struct rte_mbuf **
326 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
328 uint32_t pindex = qindex >> 4;
329 uint32_t qpos = qindex & 0xF;
331 return (port->queue_array + pindex *
332 port->qsize_sum + port->qsize_add[qpos]);
335 static inline uint16_t
336 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
338 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
340 return port->qsize[tc];
343 static inline uint16_t
344 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
346 uint16_t pipe_queue = port->pipe_queue[traffic_class];
351 static inline uint8_t
352 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
354 uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
359 static inline uint8_t
360 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
362 uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
368 pipe_profile_check(struct rte_sched_pipe_params *params,
369 uint32_t rate, uint16_t *qsize)
373 /* Pipe parameters */
374 if (params == NULL) {
376 "%s: Incorrect value for parameter params\n", __func__);
380 /* TB rate: non-zero, not greater than port rate */
381 if (params->tb_rate == 0 ||
382 params->tb_rate > rate) {
384 "%s: Incorrect value for tb rate\n", __func__);
388 /* TB size: non-zero */
389 if (params->tb_size == 0) {
391 "%s: Incorrect value for tb size\n", __func__);
395 /* TC rate: non-zero if qsize non-zero, less than pipe rate */
396 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
397 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
398 (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
399 params->tc_rate[i] > params->tb_rate))) {
401 "%s: Incorrect value for qsize or tc_rate\n", __func__);
406 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
407 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
409 "%s: Incorrect value for be traffic class rate\n", __func__);
413 /* TC period: non-zero */
414 if (params->tc_period == 0) {
416 "%s: Incorrect value for tc period\n", __func__);
420 /* Best effort tc oversubscription weight: non-zero */
421 if (params->tc_ov_weight == 0) {
423 "%s: Incorrect value for tc ov weight\n", __func__);
427 /* Queue WRR weights: non-zero */
428 for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
429 if (params->wrr_weights[i] == 0) {
431 "%s: Incorrect value for wrr weight\n", __func__);
440 rte_sched_port_check_params(struct rte_sched_port_params *params)
442 if (params == NULL) {
444 "%s: Incorrect value for parameter params\n", __func__);
449 if (params->socket < 0) {
451 "%s: Incorrect value for socket id\n", __func__);
456 if (params->rate == 0) {
458 "%s: Incorrect value for rate\n", __func__);
463 if (params->mtu == 0) {
465 "%s: Incorrect value for mtu\n", __func__);
469 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
470 if (params->n_subports_per_port == 0 ||
471 params->n_subports_per_port > 1u << 16 ||
472 !rte_is_power_of_2(params->n_subports_per_port)) {
474 "%s: Incorrect value for number of subports\n", __func__);
478 /* n_pipes_per_subport: non-zero, power of 2 */
479 if (params->n_pipes_per_subport == 0 ||
480 !rte_is_power_of_2(params->n_pipes_per_subport)) {
482 "%s: Incorrect value for maximum pipes number\n", __func__);
490 rte_sched_port_get_array_base(struct rte_sched_port_params *params, enum rte_sched_port_array array)
492 uint32_t n_subports_per_port = params->n_subports_per_port;
493 uint32_t n_pipes_per_subport = params->n_pipes_per_subport;
494 uint32_t n_pipes_per_port = n_pipes_per_subport * n_subports_per_port;
495 uint32_t n_queues_per_port = RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport * n_subports_per_port;
497 uint32_t size_subport = n_subports_per_port * sizeof(struct rte_sched_subport);
498 uint32_t size_pipe = n_pipes_per_port * sizeof(struct rte_sched_pipe);
499 uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue);
500 uint32_t size_queue_extra
501 = n_queues_per_port * sizeof(struct rte_sched_queue_extra);
502 uint32_t size_pipe_profiles
503 = params->n_max_pipe_profiles * sizeof(struct rte_sched_pipe_profile);
504 uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port);
505 uint32_t size_per_pipe_queue_array, size_queue_array;
509 size_per_pipe_queue_array = 0;
510 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
511 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
512 size_per_pipe_queue_array +=
513 params->qsize[i] * sizeof(struct rte_mbuf *);
515 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
516 params->qsize[i] * sizeof(struct rte_mbuf *);
518 size_queue_array = n_pipes_per_port * size_per_pipe_queue_array;
522 if (array == e_RTE_SCHED_PORT_ARRAY_SUBPORT)
524 base += RTE_CACHE_LINE_ROUNDUP(size_subport);
526 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE)
528 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
530 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE)
532 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
534 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA)
536 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
538 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES)
540 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
542 if (array == e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY)
544 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
546 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY)
548 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
554 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
555 enum rte_sched_subport_array array)
557 uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
558 uint32_t n_subport_pipe_queues =
559 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
561 uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
562 uint32_t size_queue =
563 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
564 uint32_t size_queue_extra
565 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
566 uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
567 sizeof(struct rte_sched_pipe_profile);
568 uint32_t size_bmp_array =
569 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
570 uint32_t size_per_pipe_queue_array, size_queue_array;
574 size_per_pipe_queue_array = 0;
575 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
576 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
577 size_per_pipe_queue_array +=
578 params->qsize[i] * sizeof(struct rte_mbuf *);
580 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
581 params->qsize[i] * sizeof(struct rte_mbuf *);
583 size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
587 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
589 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
591 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
593 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
595 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
597 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
599 if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
601 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
603 if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
605 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
607 if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
609 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
615 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
619 subport->qsize_add[0] = 0;
621 /* Strict prority traffic class */
622 for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
623 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
625 /* Best-effort traffic class */
626 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
627 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
628 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
629 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
630 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
631 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
632 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
633 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
634 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
636 subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
637 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
641 rte_sched_port_log_pipe_profile(struct rte_sched_port *port, uint32_t i)
643 struct rte_sched_pipe_profile *p = port->pipe_profiles + i;
645 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
646 " Token bucket: period = %u, credits per period = %u, size = %u\n"
647 " Traffic classes: period = %u,\n"
648 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
649 " Best-effort traffic class oversubscription: weight = %hhu\n"
650 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
655 p->tb_credits_per_period,
658 /* Traffic classes */
660 p->tc_credits_per_period[0],
661 p->tc_credits_per_period[1],
662 p->tc_credits_per_period[2],
663 p->tc_credits_per_period[3],
664 p->tc_credits_per_period[4],
665 p->tc_credits_per_period[5],
666 p->tc_credits_per_period[6],
667 p->tc_credits_per_period[7],
668 p->tc_credits_per_period[8],
669 p->tc_credits_per_period[9],
670 p->tc_credits_per_period[10],
671 p->tc_credits_per_period[11],
672 p->tc_credits_per_period[12],
674 /* Best-effort traffic class oversubscription */
678 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
681 static inline uint64_t
682 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
684 uint64_t time = time_ms;
686 time = (time * rate) / 1000;
692 rte_sched_pipe_profile_convert(struct rte_sched_port *port,
693 struct rte_sched_pipe_params *src,
694 struct rte_sched_pipe_profile *dst,
697 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
698 uint32_t lcd1, lcd2, lcd;
702 if (src->tb_rate == rate) {
703 dst->tb_credits_per_period = 1;
706 double tb_rate = (double) src->tb_rate
708 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
710 rte_approx(tb_rate, d,
711 &dst->tb_credits_per_period, &dst->tb_period);
714 dst->tb_size = src->tb_size;
716 /* Traffic Classes */
717 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
720 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
722 dst->tc_credits_per_period[i]
723 = rte_sched_time_ms_to_bytes(src->tc_period,
726 dst->tc_ov_weight = src->tc_ov_weight;
729 wrr_cost[0] = src->wrr_weights[0];
730 wrr_cost[1] = src->wrr_weights[1];
731 wrr_cost[2] = src->wrr_weights[2];
732 wrr_cost[3] = src->wrr_weights[3];
734 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
735 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
736 lcd = rte_get_lcd(lcd1, lcd2);
738 wrr_cost[0] = lcd / wrr_cost[0];
739 wrr_cost[1] = lcd / wrr_cost[1];
740 wrr_cost[2] = lcd / wrr_cost[2];
741 wrr_cost[3] = lcd / wrr_cost[3];
743 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
744 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
745 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
746 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
750 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
751 uint32_t n_max_pipes_per_subport,
756 /* Check user parameters */
757 if (params == NULL) {
759 "%s: Incorrect value for parameter params\n", __func__);
763 if (params->tb_rate == 0 || params->tb_rate > rate) {
765 "%s: Incorrect value for tb rate\n", __func__);
769 if (params->tb_size == 0) {
771 "%s: Incorrect value for tb size\n", __func__);
775 /* qsize: if non-zero, power of 2,
776 * no bigger than 32K (due to 16-bit read/write pointers)
778 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
779 uint16_t qsize = params->qsize[i];
781 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
783 "%s: Incorrect value for qsize\n", __func__);
788 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
789 uint32_t tc_rate = params->tc_rate[i];
790 uint16_t qsize = params->qsize[i];
792 if ((qsize == 0 && tc_rate != 0) ||
793 (qsize != 0 && tc_rate == 0) ||
794 (tc_rate > params->tb_rate)) {
796 "%s: Incorrect value for tc rate\n", __func__);
801 if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
802 params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
804 "%s: Incorrect qsize or tc rate(best effort)\n", __func__);
808 if (params->tc_period == 0) {
810 "%s: Incorrect value for tc period\n", __func__);
814 /* n_pipes_per_subport: non-zero, power of 2 */
815 if (params->n_pipes_per_subport_enabled == 0 ||
816 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
817 !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
819 "%s: Incorrect value for pipes number\n", __func__);
823 /* pipe_profiles and n_pipe_profiles */
824 if (params->pipe_profiles == NULL ||
825 params->n_pipe_profiles == 0 ||
826 params->n_max_pipe_profiles == 0 ||
827 params->n_pipe_profiles > params->n_max_pipe_profiles) {
829 "%s: Incorrect value for pipe profiles\n", __func__);
833 for (i = 0; i < params->n_pipe_profiles; i++) {
834 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
837 status = pipe_profile_check(p, rate, ¶ms->qsize[0]);
840 "%s: Pipe profile check failed(%d)\n", __func__, status);
849 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
851 uint32_t size0, size1;
854 status = rte_sched_port_check_params(params);
856 RTE_LOG(NOTICE, SCHED,
857 "Port scheduler params check failed (%d)\n", status);
862 size0 = sizeof(struct rte_sched_port);
863 size1 = rte_sched_port_get_array_base(params,
864 e_RTE_SCHED_PORT_ARRAY_TOTAL);
866 return size0 + size1;
869 struct rte_sched_port *
870 rte_sched_port_config(struct rte_sched_port_params *params)
872 struct rte_sched_port *port = NULL;
873 uint32_t size0, size1;
874 uint32_t cycles_per_byte;
878 status = rte_sched_port_check_params(params);
881 "%s: Port scheduler params check failed (%d)\n",
886 size0 = sizeof(struct rte_sched_port);
887 size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
889 /* Allocate memory to store the data structures */
890 port = rte_zmalloc_socket("qos_params", size0 + size1, RTE_CACHE_LINE_SIZE,
893 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
898 /* User parameters */
899 port->n_subports_per_port = params->n_subports_per_port;
900 port->n_pipes_per_subport = params->n_pipes_per_subport;
901 port->n_pipes_per_subport_log2 =
902 __builtin_ctz(params->n_pipes_per_subport);
903 port->socket = params->socket;
905 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
906 port->pipe_queue[i] = i;
908 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
909 port->pipe_tc[i] = j;
911 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
915 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
916 port->tc_queue[i] = j;
918 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
921 port->rate = params->rate;
922 port->mtu = params->mtu + params->frame_overhead;
923 port->frame_overhead = params->frame_overhead;
926 port->time_cpu_cycles = rte_get_tsc_cycles();
927 port->time_cpu_bytes = 0;
930 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
932 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
935 port->pkts_out = NULL;
936 port->n_pkts_out = 0;
942 rte_sched_subport_free(struct rte_sched_port *port,
943 struct rte_sched_subport *subport)
945 uint32_t n_subport_pipe_queues;
951 n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
953 /* Free enqueued mbufs */
954 for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
955 struct rte_mbuf **mbufs =
956 rte_sched_subport_pipe_qbase(subport, qindex);
957 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
959 struct rte_sched_queue *queue = subport->queue + qindex;
960 uint16_t qr = queue->qr & (qsize - 1);
961 uint16_t qw = queue->qw & (qsize - 1);
963 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
964 rte_pktmbuf_free(mbufs[qr]);
968 rte_bitmap_free(subport->bmp);
972 rte_sched_port_free(struct rte_sched_port *port)
976 /* Check user parameters */
980 for (i = 0; i < port->n_subports_per_port; i++)
981 rte_sched_subport_free(port, port->subports[i]);
987 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
989 struct rte_sched_subport *s = port->subports[i];
991 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
992 " Token bucket: period = %u, credits per period = %u, size = %u\n"
993 " Traffic classes: period = %u\n"
994 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
995 " Best effort traffic class oversubscription: wm min = %u, wm max = %u\n",
1000 s->tb_credits_per_period,
1003 /* Traffic classes */
1005 s->tc_credits_per_period[0],
1006 s->tc_credits_per_period[1],
1007 s->tc_credits_per_period[2],
1008 s->tc_credits_per_period[3],
1009 s->tc_credits_per_period[4],
1010 s->tc_credits_per_period[5],
1011 s->tc_credits_per_period[6],
1012 s->tc_credits_per_period[7],
1013 s->tc_credits_per_period[8],
1014 s->tc_credits_per_period[9],
1015 s->tc_credits_per_period[10],
1016 s->tc_credits_per_period[11],
1017 s->tc_credits_per_period[12],
1019 /* Best effort traffic class oversubscription */
1025 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1029 for (i = 0; i < n_subports; i++) {
1030 struct rte_sched_subport *subport = port->subports[i];
1032 rte_sched_subport_free(port, subport);
1039 rte_sched_subport_config(struct rte_sched_port *port,
1040 uint32_t subport_id,
1041 struct rte_sched_subport_params *params)
1043 struct rte_sched_subport *s = NULL;
1044 uint32_t n_subports = subport_id;
1045 uint32_t n_subport_pipe_queues, i;
1046 uint32_t size0, size1, bmp_mem_size;
1049 /* Check user parameters */
1052 "%s: Incorrect value for parameter port\n", __func__);
1056 if (subport_id >= port->n_subports_per_port) {
1058 "%s: Incorrect value for subport id\n", __func__);
1060 rte_sched_free_memory(port, n_subports);
1064 status = rte_sched_subport_check_params(params,
1065 port->n_pipes_per_subport,
1068 RTE_LOG(NOTICE, SCHED,
1069 "%s: Port scheduler params check failed (%d)\n",
1072 rte_sched_free_memory(port, n_subports);
1076 /* Determine the amount of memory to allocate */
1077 size0 = sizeof(struct rte_sched_subport);
1078 size1 = rte_sched_subport_get_array_base(params,
1079 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1081 /* Allocate memory to store the data structures */
1082 s = rte_zmalloc_socket("subport_params", size0 + size1,
1083 RTE_CACHE_LINE_SIZE, port->socket);
1086 "%s: Memory allocation fails\n", __func__);
1088 rte_sched_free_memory(port, n_subports);
1095 port->subports[subport_id] = s;
1097 /* Token Bucket (TB) */
1098 if (params->tb_rate == port->rate) {
1099 s->tb_credits_per_period = 1;
1102 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
1103 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
1105 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
1108 s->tb_size = params->tb_size;
1109 s->tb_time = port->time;
1110 s->tb_credits = s->tb_size / 2;
1112 /* Traffic Classes (TCs) */
1113 s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
1114 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1115 if (params->qsize[i])
1116 s->tc_credits_per_period[i]
1117 = rte_sched_time_ms_to_bytes(params->tc_period,
1118 params->tc_rate[i]);
1120 s->tc_time = port->time + s->tc_period;
1121 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1122 if (params->qsize[i])
1123 s->tc_credits[i] = s->tc_credits_per_period[i];
1125 /* compile time checks */
1126 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1127 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1128 (RTE_SCHED_PORT_N_GRINDERS - 1));
1130 /* User parameters */
1131 s->n_pipes_per_subport_enabled = params->n_pipes_per_subport_enabled;
1132 memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1133 s->n_pipe_profiles = params->n_pipe_profiles;
1134 s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1136 #ifdef RTE_SCHED_RED
1137 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1140 for (j = 0; j < RTE_COLORS; j++) {
1141 /* if min/max are both zero, then RED is disabled */
1142 if ((params->red_params[i][j].min_th |
1143 params->red_params[i][j].max_th) == 0) {
1147 if (rte_red_config_init(&s->red_config[i][j],
1148 params->red_params[i][j].wq_log2,
1149 params->red_params[i][j].min_th,
1150 params->red_params[i][j].max_th,
1151 params->red_params[i][j].maxp_inv) != 0) {
1152 rte_sched_free_memory(port, n_subports);
1154 RTE_LOG(NOTICE, SCHED,
1155 "%s: RED configuration init fails\n", __func__);
1162 /* Scheduling loop detection */
1163 s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1164 s->pipe_exhaustion = 0;
1167 s->busy_grinders = 0;
1169 /* Queue base calculation */
1170 rte_sched_subport_config_qsize(s);
1172 /* Large data structures */
1173 s->pipe = (struct rte_sched_pipe *)
1174 (s->memory + rte_sched_subport_get_array_base(params,
1175 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1176 s->queue = (struct rte_sched_queue *)
1177 (s->memory + rte_sched_subport_get_array_base(params,
1178 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1179 s->queue_extra = (struct rte_sched_queue_extra *)
1180 (s->memory + rte_sched_subport_get_array_base(params,
1181 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1182 s->pipe_profiles = (struct rte_sched_pipe_profile *)
1183 (s->memory + rte_sched_subport_get_array_base(params,
1184 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1185 s->bmp_array = s->memory + rte_sched_subport_get_array_base(params,
1186 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1187 s->queue_array = (struct rte_mbuf **)
1188 (s->memory + rte_sched_subport_get_array_base(params,
1189 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1192 n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1193 bmp_mem_size = rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
1194 s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1196 if (s->bmp == NULL) {
1198 "%s: Subport bitmap init error\n", __func__);
1200 rte_sched_free_memory(port, n_subports);
1204 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1205 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1207 #ifdef RTE_SCHED_SUBPORT_TC_OV
1208 /* TC oversubscription */
1209 s->tc_ov_wm_min = port->mtu;
1210 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
1211 s->pipe_tc_be_rate_max);
1212 s->tc_ov_wm = s->tc_ov_wm_max;
1213 s->tc_ov_period_id = 0;
1219 rte_sched_port_log_subport_config(port, subport_id);
1225 rte_sched_pipe_config(struct rte_sched_port *port,
1226 uint32_t subport_id,
1228 int32_t pipe_profile)
1230 struct rte_sched_subport *s;
1231 struct rte_sched_pipe *p;
1232 struct rte_sched_pipe_profile *params;
1233 uint32_t deactivate, profile, i;
1235 /* Check user parameters */
1236 profile = (uint32_t) pipe_profile;
1237 deactivate = (pipe_profile < 0);
1241 "%s: Incorrect value for parameter port\n", __func__);
1245 if (subport_id >= port->n_subports_per_port) {
1247 "%s: Incorrect value for parameter subport id\n", __func__);
1251 if (pipe_id >= port->n_pipes_per_subport) {
1253 "%s: Incorrect value for parameter pipe id\n", __func__);
1257 if (!deactivate && profile >= port->n_pipe_profiles) {
1259 "%s: Incorrect value for parameter pipe profile\n", __func__);
1263 /* Check that subport configuration is valid */
1264 s = port->subport + subport_id;
1265 if (s->tb_period == 0) {
1267 "%s: Subport configuration invalid\n", __func__);
1271 p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
1273 /* Handle the case when pipe already has a valid configuration */
1275 params = port->pipe_profiles + p->profile;
1277 double subport_tc_be_rate =
1278 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1279 / (double) s->tc_period;
1280 double pipe_tc_be_rate =
1281 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1282 / (double) params->tc_period;
1283 uint32_t tc_be_ov = s->tc_ov;
1285 /* Unplug pipe from its subport */
1286 s->tc_ov_n -= params->tc_ov_weight;
1287 s->tc_ov_rate -= pipe_tc_be_rate;
1288 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1290 if (s->tc_ov != tc_be_ov) {
1291 RTE_LOG(DEBUG, SCHED,
1292 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1293 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1296 /* Reset the pipe */
1297 memset(p, 0, sizeof(struct rte_sched_pipe));
1303 /* Apply the new pipe configuration */
1304 p->profile = profile;
1305 params = port->pipe_profiles + p->profile;
1307 /* Token Bucket (TB) */
1308 p->tb_time = port->time;
1309 p->tb_credits = params->tb_size / 2;
1311 /* Traffic Classes (TCs) */
1312 p->tc_time = port->time + params->tc_period;
1314 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1316 p->tc_credits[i] = params->tc_credits_per_period[i];
1319 /* Subport best effort tc oversubscription */
1320 double subport_tc_be_rate =
1321 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1322 / (double) s->tc_period;
1323 double pipe_tc_be_rate =
1324 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1325 / (double) params->tc_period;
1326 uint32_t tc_be_ov = s->tc_ov;
1328 s->tc_ov_n += params->tc_ov_weight;
1329 s->tc_ov_rate += pipe_tc_be_rate;
1330 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1332 if (s->tc_ov != tc_be_ov) {
1333 RTE_LOG(DEBUG, SCHED,
1334 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1335 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1337 p->tc_ov_period_id = s->tc_ov_period_id;
1338 p->tc_ov_credits = s->tc_ov_wm;
1345 rte_sched_port_pipe_profile_add(struct rte_sched_port *port,
1346 struct rte_sched_pipe_params *params,
1347 uint32_t *pipe_profile_id)
1349 struct rte_sched_pipe_profile *pp;
1356 "%s: Incorrect value for parameter port\n", __func__);
1360 /* Pipe profiles not exceeds the max limit */
1361 if (port->n_pipe_profiles >= port->n_max_pipe_profiles) {
1363 "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1368 status = pipe_profile_check(params, port->rate, &port->qsize[0]);
1371 "%s: Pipe profile check failed(%d)\n", __func__, status);
1375 pp = &port->pipe_profiles[port->n_pipe_profiles];
1376 rte_sched_pipe_profile_convert(port, params, pp, port->rate);
1378 /* Pipe profile not exists */
1379 for (i = 0; i < port->n_pipe_profiles; i++)
1380 if (memcmp(port->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1382 "%s: Pipe profile doesn't exist\n", __func__);
1386 /* Pipe profile commit */
1387 *pipe_profile_id = port->n_pipe_profiles;
1388 port->n_pipe_profiles++;
1390 if (port->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1391 port->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1393 rte_sched_port_log_pipe_profile(port, *pipe_profile_id);
1398 static inline uint32_t
1399 rte_sched_port_qindex(struct rte_sched_port *port,
1402 uint32_t traffic_class,
1405 return ((subport & (port->n_subports_per_port - 1)) <<
1406 (port->n_pipes_per_subport_log2 + 4)) |
1407 ((pipe & (port->n_pipes_per_subport - 1)) << 4) |
1408 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1409 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1413 rte_sched_port_pkt_write(struct rte_sched_port *port,
1414 struct rte_mbuf *pkt,
1415 uint32_t subport, uint32_t pipe,
1416 uint32_t traffic_class,
1417 uint32_t queue, enum rte_color color)
1420 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1422 rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1426 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1427 const struct rte_mbuf *pkt,
1428 uint32_t *subport, uint32_t *pipe,
1429 uint32_t *traffic_class, uint32_t *queue)
1431 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1433 *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1434 *pipe = (queue_id >> 4) & (port->n_pipes_per_subport - 1);
1435 *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1436 *queue = rte_sched_port_tc_queue(port, queue_id);
1440 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1442 return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1446 rte_sched_subport_read_stats(struct rte_sched_port *port,
1447 uint32_t subport_id,
1448 struct rte_sched_subport_stats *stats,
1451 struct rte_sched_subport *s;
1453 /* Check user parameters */
1456 "%s: Incorrect value for parameter port\n", __func__);
1460 if (subport_id >= port->n_subports_per_port) {
1462 "%s: Incorrect value for subport id\n", __func__);
1466 if (stats == NULL) {
1468 "%s: Incorrect value for parameter stats\n", __func__);
1472 if (tc_ov == NULL) {
1474 "%s: Incorrect value for tc_ov\n", __func__);
1478 s = port->subport + subport_id;
1480 /* Copy subport stats and clear */
1481 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1482 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1484 /* Subport TC oversubscription status */
1491 rte_sched_queue_read_stats(struct rte_sched_port *port,
1493 struct rte_sched_queue_stats *stats,
1496 struct rte_sched_queue *q;
1497 struct rte_sched_queue_extra *qe;
1499 /* Check user parameters */
1502 "%s: Incorrect value for parameter port\n", __func__);
1506 if (queue_id >= rte_sched_port_queues_per_port(port)) {
1508 "%s: Incorrect value for queue id\n", __func__);
1512 if (stats == NULL) {
1514 "%s: Incorrect value for parameter stats\n", __func__);
1520 "%s: Incorrect value for parameter qlen\n", __func__);
1523 q = port->queue + queue_id;
1524 qe = port->queue_extra + queue_id;
1526 /* Copy queue stats and clear */
1527 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1528 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1531 *qlen = q->qw - q->qr;
1536 #ifdef RTE_SCHED_DEBUG
1539 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1541 struct rte_sched_queue *queue = port->queue + qindex;
1543 return queue->qr == queue->qw;
1546 #endif /* RTE_SCHED_DEBUG */
1548 #ifdef RTE_SCHED_COLLECT_STATS
1551 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1553 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1554 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1555 uint32_t pkt_len = pkt->pkt_len;
1557 s->stats.n_pkts_tc[tc_index] += 1;
1558 s->stats.n_bytes_tc[tc_index] += pkt_len;
1561 #ifdef RTE_SCHED_RED
1563 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1565 struct rte_mbuf *pkt, uint32_t red)
1568 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1570 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1573 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1574 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1575 uint32_t pkt_len = pkt->pkt_len;
1577 s->stats.n_pkts_tc_dropped[tc_index] += 1;
1578 s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1579 #ifdef RTE_SCHED_RED
1580 s->stats.n_pkts_red_dropped[tc_index] += red;
1585 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1587 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1588 uint32_t pkt_len = pkt->pkt_len;
1590 qe->stats.n_pkts += 1;
1591 qe->stats.n_bytes += pkt_len;
1594 #ifdef RTE_SCHED_RED
1596 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1598 struct rte_mbuf *pkt, uint32_t red)
1601 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1603 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1606 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1607 uint32_t pkt_len = pkt->pkt_len;
1609 qe->stats.n_pkts_dropped += 1;
1610 qe->stats.n_bytes_dropped += pkt_len;
1611 #ifdef RTE_SCHED_RED
1612 qe->stats.n_pkts_red_dropped += red;
1616 #endif /* RTE_SCHED_COLLECT_STATS */
1618 #ifdef RTE_SCHED_RED
1621 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1623 struct rte_sched_queue_extra *qe;
1624 struct rte_red_config *red_cfg;
1625 struct rte_red *red;
1627 enum rte_color color;
1629 tc_index = rte_sched_port_pipe_tc(port, qindex);
1630 color = rte_sched_port_pkt_read_color(pkt);
1631 red_cfg = &port->red_config[tc_index][color];
1633 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1636 qe = port->queue_extra + qindex;
1639 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1643 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1645 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1646 struct rte_red *red = &qe->red;
1648 rte_red_mark_queue_empty(red, port->time);
1653 #define rte_sched_port_red_drop(port, pkt, qindex, qlen) 0
1655 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1657 #endif /* RTE_SCHED_RED */
1659 #ifdef RTE_SCHED_DEBUG
1662 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1669 rte_panic("Empty slab at position %u\n", bmp_pos);
1672 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1673 if (mask & bmp_slab) {
1674 if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1675 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1682 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1686 #endif /* RTE_SCHED_DEBUG */
1688 static inline uint32_t
1689 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port,
1690 struct rte_mbuf *pkt)
1692 struct rte_sched_queue *q;
1693 #ifdef RTE_SCHED_COLLECT_STATS
1694 struct rte_sched_queue_extra *qe;
1696 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1698 q = port->queue + qindex;
1700 #ifdef RTE_SCHED_COLLECT_STATS
1701 qe = port->queue_extra + qindex;
1709 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1710 uint32_t qindex, struct rte_mbuf **qbase)
1712 struct rte_sched_queue *q;
1713 struct rte_mbuf **q_qw;
1716 q = port->queue + qindex;
1717 qsize = rte_sched_port_qsize(port, qindex);
1718 q_qw = qbase + (q->qw & (qsize - 1));
1720 rte_prefetch0(q_qw);
1721 rte_bitmap_prefetch0(port->bmp, qindex);
1725 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex,
1726 struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1728 struct rte_sched_queue *q;
1732 q = port->queue + qindex;
1733 qsize = rte_sched_port_qsize(port, qindex);
1734 qlen = q->qw - q->qr;
1736 /* Drop the packet (and update drop stats) when queue is full */
1737 if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) ||
1739 rte_pktmbuf_free(pkt);
1740 #ifdef RTE_SCHED_COLLECT_STATS
1741 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt,
1743 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt,
1749 /* Enqueue packet */
1750 qbase[q->qw & (qsize - 1)] = pkt;
1753 /* Activate queue in the port bitmap */
1754 rte_bitmap_set(port->bmp, qindex);
1757 #ifdef RTE_SCHED_COLLECT_STATS
1758 rte_sched_port_update_subport_stats(port, qindex, pkt);
1759 rte_sched_port_update_queue_stats(port, qindex, pkt);
1767 * The enqueue function implements a 4-level pipeline with each stage
1768 * processing two different packets. The purpose of using a pipeline
1769 * is to hide the latency of prefetching the data structures. The
1770 * naming convention is presented in the diagram below:
1772 * p00 _______ p10 _______ p20 _______ p30 _______
1773 * ----->| |----->| |----->| |----->| |----->
1774 * | 0 | | 1 | | 2 | | 3 |
1775 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1780 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1783 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1784 *pkt30, *pkt31, *pkt_last;
1785 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1786 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1787 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1788 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1794 * Less then 6 input packets available, which is not enough to
1797 if (unlikely(n_pkts < 6)) {
1798 struct rte_mbuf **q_base[5];
1801 /* Prefetch the mbuf structure of each packet */
1802 for (i = 0; i < n_pkts; i++)
1803 rte_prefetch0(pkts[i]);
1805 /* Prefetch the queue structure for each queue */
1806 for (i = 0; i < n_pkts; i++)
1807 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port,
1810 /* Prefetch the write pointer location of each queue */
1811 for (i = 0; i < n_pkts; i++) {
1812 q_base[i] = rte_sched_port_qbase(port, q[i]);
1813 rte_sched_port_enqueue_qwa_prefetch0(port, q[i],
1817 /* Write each packet to its queue */
1818 for (i = 0; i < n_pkts; i++)
1819 result += rte_sched_port_enqueue_qwa(port, q[i],
1820 q_base[i], pkts[i]);
1825 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1828 rte_prefetch0(pkt20);
1829 rte_prefetch0(pkt21);
1833 rte_prefetch0(pkt10);
1834 rte_prefetch0(pkt11);
1836 q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1837 q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1841 rte_prefetch0(pkt00);
1842 rte_prefetch0(pkt01);
1844 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1845 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1847 q20_base = rte_sched_port_qbase(port, q20);
1848 q21_base = rte_sched_port_qbase(port, q21);
1849 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1850 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1852 /* Run the pipeline */
1853 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1854 /* Propagate stage inputs */
1865 q30_base = q20_base;
1866 q31_base = q21_base;
1868 /* Stage 0: Get packets in */
1870 pkt01 = pkts[i + 1];
1871 rte_prefetch0(pkt00);
1872 rte_prefetch0(pkt01);
1874 /* Stage 1: Prefetch queue structure storing queue pointers */
1875 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1876 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1878 /* Stage 2: Prefetch queue write location */
1879 q20_base = rte_sched_port_qbase(port, q20);
1880 q21_base = rte_sched_port_qbase(port, q21);
1881 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1882 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1884 /* Stage 3: Write packet to queue and activate queue */
1885 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1886 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1887 result += r30 + r31;
1891 * Drain the pipeline (exactly 6 packets).
1892 * Handle the last packet in the case
1893 * of an odd number of input packets.
1895 pkt_last = pkts[n_pkts - 1];
1896 rte_prefetch0(pkt_last);
1898 q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1899 q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1901 q10_base = rte_sched_port_qbase(port, q10);
1902 q11_base = rte_sched_port_qbase(port, q11);
1903 rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1904 rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1906 r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1907 r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1908 result += r20 + r21;
1910 q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1912 q00_base = rte_sched_port_qbase(port, q00);
1913 q01_base = rte_sched_port_qbase(port, q01);
1914 rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1915 rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1917 r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1918 r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1919 result += r10 + r11;
1921 q_last_base = rte_sched_port_qbase(port, q_last);
1922 rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1924 r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1925 r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1926 result += r00 + r01;
1929 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1936 #ifndef RTE_SCHED_SUBPORT_TC_OV
1939 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1941 struct rte_sched_grinder *grinder = port->grinder + pos;
1942 struct rte_sched_subport *subport = grinder->subport;
1943 struct rte_sched_pipe *pipe = grinder->pipe;
1944 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1949 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1950 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1951 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1952 subport->tb_time += n_periods * subport->tb_period;
1955 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1956 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1957 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1958 pipe->tb_time += n_periods * params->tb_period;
1961 if (unlikely(port->time >= subport->tc_time)) {
1962 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1963 subport->tc_credits[i] = subport->tc_credits_per_period[i];
1965 subport->tc_time = port->time + subport->tc_period;
1969 if (unlikely(port->time >= pipe->tc_time)) {
1970 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1971 pipe->tc_credits[i] = params->tc_credits_per_period[i];
1973 pipe->tc_time = port->time + params->tc_period;
1979 static inline uint32_t
1980 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1982 struct rte_sched_grinder *grinder = port->grinder + pos;
1983 struct rte_sched_subport *subport = grinder->subport;
1984 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1985 uint32_t tc_consumption = 0, tc_ov_consumption_max;
1986 uint32_t tc_ov_wm = subport->tc_ov_wm;
1989 if (subport->tc_ov == 0)
1990 return subport->tc_ov_wm_max;
1992 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
1993 tc_ov_consumption[i] =
1994 subport->tc_credits_per_period[i] - subport->tc_credits[i];
1995 tc_consumption += tc_ov_consumption[i];
1998 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
1999 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2000 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2002 tc_ov_consumption_max =
2003 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2006 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2007 (tc_ov_consumption_max - port->mtu)) {
2008 tc_ov_wm -= tc_ov_wm >> 7;
2009 if (tc_ov_wm < subport->tc_ov_wm_min)
2010 tc_ov_wm = subport->tc_ov_wm_min;
2015 tc_ov_wm += (tc_ov_wm >> 7) + 1;
2016 if (tc_ov_wm > subport->tc_ov_wm_max)
2017 tc_ov_wm = subport->tc_ov_wm_max;
2023 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
2025 struct rte_sched_grinder *grinder = port->grinder + pos;
2026 struct rte_sched_subport *subport = grinder->subport;
2027 struct rte_sched_pipe *pipe = grinder->pipe;
2028 struct rte_sched_pipe_profile *params = grinder->pipe_params;
2033 n_periods = (port->time - subport->tb_time) / subport->tb_period;
2034 subport->tb_credits += n_periods * subport->tb_credits_per_period;
2035 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
2036 subport->tb_time += n_periods * subport->tb_period;
2039 n_periods = (port->time - pipe->tb_time) / params->tb_period;
2040 pipe->tb_credits += n_periods * params->tb_credits_per_period;
2041 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
2042 pipe->tb_time += n_periods * params->tb_period;
2045 if (unlikely(port->time >= subport->tc_time)) {
2046 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
2048 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2049 subport->tc_credits[i] = subport->tc_credits_per_period[i];
2051 subport->tc_time = port->time + subport->tc_period;
2052 subport->tc_ov_period_id++;
2056 if (unlikely(port->time >= pipe->tc_time)) {
2057 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2058 pipe->tc_credits[i] = params->tc_credits_per_period[i];
2059 pipe->tc_time = port->time + params->tc_period;
2062 /* Pipe TCs - Oversubscription */
2063 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2064 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2066 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2070 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
2073 #ifndef RTE_SCHED_SUBPORT_TC_OV
2076 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
2078 struct rte_sched_grinder *grinder = port->grinder + pos;
2079 struct rte_sched_subport *subport = grinder->subport;
2080 struct rte_sched_pipe *pipe = grinder->pipe;
2081 struct rte_mbuf *pkt = grinder->pkt;
2082 uint32_t tc_index = grinder->tc_index;
2083 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2084 uint32_t subport_tb_credits = subport->tb_credits;
2085 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2086 uint32_t pipe_tb_credits = pipe->tb_credits;
2087 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2090 /* Check queue credits */
2091 enough_credits = (pkt_len <= subport_tb_credits) &&
2092 (pkt_len <= subport_tc_credits) &&
2093 (pkt_len <= pipe_tb_credits) &&
2094 (pkt_len <= pipe_tc_credits);
2096 if (!enough_credits)
2099 /* Update port credits */
2100 subport->tb_credits -= pkt_len;
2101 subport->tc_credits[tc_index] -= pkt_len;
2102 pipe->tb_credits -= pkt_len;
2103 pipe->tc_credits[tc_index] -= pkt_len;
2111 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
2113 struct rte_sched_grinder *grinder = port->grinder + pos;
2114 struct rte_sched_subport *subport = grinder->subport;
2115 struct rte_sched_pipe *pipe = grinder->pipe;
2116 struct rte_mbuf *pkt = grinder->pkt;
2117 uint32_t tc_index = grinder->tc_index;
2118 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2119 uint32_t subport_tb_credits = subport->tb_credits;
2120 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2121 uint32_t pipe_tb_credits = pipe->tb_credits;
2122 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2123 uint32_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2124 uint32_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2125 uint32_t pipe_tc_ov_credits, i;
2128 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2129 pipe_tc_ov_mask1[i] = UINT32_MAX;
2131 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2132 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = UINT32_MAX;
2133 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2135 /* Check pipe and subport credits */
2136 enough_credits = (pkt_len <= subport_tb_credits) &&
2137 (pkt_len <= subport_tc_credits) &&
2138 (pkt_len <= pipe_tb_credits) &&
2139 (pkt_len <= pipe_tc_credits) &&
2140 (pkt_len <= pipe_tc_ov_credits);
2142 if (!enough_credits)
2145 /* Update pipe and subport credits */
2146 subport->tb_credits -= pkt_len;
2147 subport->tc_credits[tc_index] -= pkt_len;
2148 pipe->tb_credits -= pkt_len;
2149 pipe->tc_credits[tc_index] -= pkt_len;
2150 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2155 #endif /* RTE_SCHED_SUBPORT_TC_OV */
2159 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
2161 struct rte_sched_grinder *grinder = port->grinder + pos;
2162 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2163 struct rte_mbuf *pkt = grinder->pkt;
2164 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2165 uint32_t be_tc_active;
2167 if (!grinder_credits_check(port, pos))
2170 /* Advance port time */
2171 port->time += pkt_len;
2174 port->pkts_out[port->n_pkts_out++] = pkt;
2177 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2178 grinder->wrr_tokens[grinder->qpos] +=
2179 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2181 if (queue->qr == queue->qw) {
2182 uint32_t qindex = grinder->qindex[grinder->qpos];
2184 rte_bitmap_clear(port->bmp, qindex);
2185 grinder->qmask &= ~(1 << grinder->qpos);
2187 grinder->wrr_mask[grinder->qpos] = 0;
2188 rte_sched_port_set_queue_empty_timestamp(port, qindex);
2191 /* Reset pipe loop detection */
2192 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2193 grinder->productive = 1;
2198 #ifdef SCHED_VECTOR_SSE4
2201 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2203 __m128i index = _mm_set1_epi32(base_pipe);
2204 __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
2205 __m128i res = _mm_cmpeq_epi32(pipes, index);
2207 pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
2208 pipes = _mm_cmpeq_epi32(pipes, index);
2209 res = _mm_or_si128(res, pipes);
2211 if (_mm_testz_si128(res, res))
2217 #elif defined(SCHED_VECTOR_NEON)
2220 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2222 uint32x4_t index, pipes;
2223 uint32_t *pos = (uint32_t *)port->grinder_base_bmp_pos;
2225 index = vmovq_n_u32(base_pipe);
2226 pipes = vld1q_u32(pos);
2227 if (!vminvq_u32(veorq_u32(pipes, index)))
2230 pipes = vld1q_u32(pos + 4);
2231 if (!vminvq_u32(veorq_u32(pipes, index)))
2240 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2244 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2245 if (port->grinder_base_bmp_pos[i] == base_pipe)
2252 #endif /* RTE_SCHED_OPTIMIZATIONS */
2255 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2257 struct rte_sched_grinder *grinder = port->grinder + pos;
2260 grinder->pcache_w = 0;
2261 grinder->pcache_r = 0;
2263 w[0] = (uint16_t) bmp_slab;
2264 w[1] = (uint16_t) (bmp_slab >> 16);
2265 w[2] = (uint16_t) (bmp_slab >> 32);
2266 w[3] = (uint16_t) (bmp_slab >> 48);
2268 grinder->pcache_qmask[grinder->pcache_w] = w[0];
2269 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2270 grinder->pcache_w += (w[0] != 0);
2272 grinder->pcache_qmask[grinder->pcache_w] = w[1];
2273 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2274 grinder->pcache_w += (w[1] != 0);
2276 grinder->pcache_qmask[grinder->pcache_w] = w[2];
2277 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2278 grinder->pcache_w += (w[2] != 0);
2280 grinder->pcache_qmask[grinder->pcache_w] = w[3];
2281 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2282 grinder->pcache_w += (w[3] != 0);
2286 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
2288 struct rte_sched_grinder *grinder = port->grinder + pos;
2291 grinder->tccache_w = 0;
2292 grinder->tccache_r = 0;
2294 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2295 b = (uint8_t) ((qmask >> i) & 0x1);
2296 grinder->tccache_qmask[grinder->tccache_w] = b;
2297 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2298 grinder->tccache_w += (b != 0);
2301 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2302 grinder->tccache_qmask[grinder->tccache_w] = b;
2303 grinder->tccache_qindex[grinder->tccache_w] = qindex +
2304 RTE_SCHED_TRAFFIC_CLASS_BE;
2305 grinder->tccache_w += (b != 0);
2309 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
2311 struct rte_sched_grinder *grinder = port->grinder + pos;
2312 struct rte_mbuf **qbase;
2316 if (grinder->tccache_r == grinder->tccache_w)
2319 qindex = grinder->tccache_qindex[grinder->tccache_r];
2320 qbase = rte_sched_port_qbase(port, qindex);
2321 qsize = rte_sched_port_qsize(port, qindex);
2323 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2324 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2325 grinder->qsize = qsize;
2327 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2328 grinder->queue[0] = port->queue + qindex;
2329 grinder->qbase[0] = qbase;
2330 grinder->qindex[0] = qindex;
2331 grinder->tccache_r++;
2336 grinder->queue[0] = port->queue + qindex;
2337 grinder->queue[1] = port->queue + qindex + 1;
2338 grinder->queue[2] = port->queue + qindex + 2;
2339 grinder->queue[3] = port->queue + qindex + 3;
2341 grinder->qbase[0] = qbase;
2342 grinder->qbase[1] = qbase + qsize;
2343 grinder->qbase[2] = qbase + 2 * qsize;
2344 grinder->qbase[3] = qbase + 3 * qsize;
2346 grinder->qindex[0] = qindex;
2347 grinder->qindex[1] = qindex + 1;
2348 grinder->qindex[2] = qindex + 2;
2349 grinder->qindex[3] = qindex + 3;
2351 grinder->tccache_r++;
2356 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
2358 struct rte_sched_grinder *grinder = port->grinder + pos;
2359 uint32_t pipe_qindex;
2360 uint16_t pipe_qmask;
2362 if (grinder->pcache_r < grinder->pcache_w) {
2363 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2364 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2365 grinder->pcache_r++;
2367 uint64_t bmp_slab = 0;
2368 uint32_t bmp_pos = 0;
2370 /* Get another non-empty pipe group */
2371 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
2374 #ifdef RTE_SCHED_DEBUG
2375 debug_check_queue_slab(port, bmp_pos, bmp_slab);
2378 /* Return if pipe group already in one of the other grinders */
2379 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2380 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
2383 port->grinder_base_bmp_pos[pos] = bmp_pos;
2385 /* Install new pipe group into grinder's pipe cache */
2386 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
2388 pipe_qmask = grinder->pcache_qmask[0];
2389 pipe_qindex = grinder->pcache_qindex[0];
2390 grinder->pcache_r = 1;
2393 /* Install new pipe in the grinder */
2394 grinder->pindex = pipe_qindex >> 4;
2395 grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
2396 grinder->pipe = port->pipe + grinder->pindex;
2397 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2398 grinder->productive = 0;
2400 grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
2401 grinder_next_tc(port, pos);
2403 /* Check for pipe exhaustion */
2404 if (grinder->pindex == port->pipe_loop) {
2405 port->pipe_exhaustion = 1;
2406 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2414 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
2416 struct rte_sched_grinder *grinder = port->grinder + pos;
2417 struct rte_sched_pipe *pipe = grinder->pipe;
2418 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2419 uint32_t qmask = grinder->qmask;
2421 grinder->wrr_tokens[0] =
2422 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2423 grinder->wrr_tokens[1] =
2424 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2425 grinder->wrr_tokens[2] =
2426 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2427 grinder->wrr_tokens[3] =
2428 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2430 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2431 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2432 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2433 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2435 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2436 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2437 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2438 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2442 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
2444 struct rte_sched_grinder *grinder = port->grinder + pos;
2445 struct rte_sched_pipe *pipe = grinder->pipe;
2447 pipe->wrr_tokens[0] =
2448 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2449 RTE_SCHED_WRR_SHIFT;
2450 pipe->wrr_tokens[1] =
2451 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2452 RTE_SCHED_WRR_SHIFT;
2453 pipe->wrr_tokens[2] =
2454 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2455 RTE_SCHED_WRR_SHIFT;
2456 pipe->wrr_tokens[3] =
2457 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2458 RTE_SCHED_WRR_SHIFT;
2462 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
2464 struct rte_sched_grinder *grinder = port->grinder + pos;
2465 uint16_t wrr_tokens_min;
2467 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2468 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2469 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2470 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2472 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2473 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2475 grinder->wrr_tokens[0] -= wrr_tokens_min;
2476 grinder->wrr_tokens[1] -= wrr_tokens_min;
2477 grinder->wrr_tokens[2] -= wrr_tokens_min;
2478 grinder->wrr_tokens[3] -= wrr_tokens_min;
2482 #define grinder_evict(port, pos)
2485 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
2487 struct rte_sched_grinder *grinder = port->grinder + pos;
2489 rte_prefetch0(grinder->pipe);
2490 rte_prefetch0(grinder->queue[0]);
2494 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
2496 struct rte_sched_grinder *grinder = port->grinder + pos;
2497 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2499 qsize = grinder->qsize;
2502 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2503 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2505 rte_prefetch0(grinder->qbase[0] + qr[0]);
2509 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2510 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2511 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2512 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2514 rte_prefetch0(grinder->qbase[0] + qr[0]);
2515 rte_prefetch0(grinder->qbase[1] + qr[1]);
2517 grinder_wrr_load(port, pos);
2518 grinder_wrr(port, pos);
2520 rte_prefetch0(grinder->qbase[2] + qr[2]);
2521 rte_prefetch0(grinder->qbase[3] + qr[3]);
2525 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2527 struct rte_sched_grinder *grinder = port->grinder + pos;
2528 uint32_t qpos = grinder->qpos;
2529 struct rte_mbuf **qbase = grinder->qbase[qpos];
2530 uint16_t qsize = grinder->qsize;
2531 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2533 grinder->pkt = qbase[qr];
2534 rte_prefetch0(grinder->pkt);
2536 if (unlikely((qr & 0x7) == 7)) {
2537 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2539 rte_prefetch0(qbase + qr_next);
2543 static inline uint32_t
2544 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2546 struct rte_sched_grinder *grinder = port->grinder + pos;
2548 switch (grinder->state) {
2549 case e_GRINDER_PREFETCH_PIPE:
2551 if (grinder_next_pipe(port, pos)) {
2552 grinder_prefetch_pipe(port, pos);
2553 port->busy_grinders++;
2555 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2562 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2564 struct rte_sched_pipe *pipe = grinder->pipe;
2566 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2567 grinder_prefetch_tc_queue_arrays(port, pos);
2568 grinder_credits_update(port, pos);
2570 grinder->state = e_GRINDER_PREFETCH_MBUF;
2574 case e_GRINDER_PREFETCH_MBUF:
2576 grinder_prefetch_mbuf(port, pos);
2578 grinder->state = e_GRINDER_READ_MBUF;
2582 case e_GRINDER_READ_MBUF:
2584 uint32_t wrr_active, result = 0;
2586 result = grinder_schedule(port, pos);
2588 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2590 /* Look for next packet within the same TC */
2591 if (result && grinder->qmask) {
2593 grinder_wrr(port, pos);
2595 grinder_prefetch_mbuf(port, pos);
2601 grinder_wrr_store(port, pos);
2603 /* Look for another active TC within same pipe */
2604 if (grinder_next_tc(port, pos)) {
2605 grinder_prefetch_tc_queue_arrays(port, pos);
2607 grinder->state = e_GRINDER_PREFETCH_MBUF;
2611 if (grinder->productive == 0 &&
2612 port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2613 port->pipe_loop = grinder->pindex;
2615 grinder_evict(port, pos);
2617 /* Look for another active pipe */
2618 if (grinder_next_pipe(port, pos)) {
2619 grinder_prefetch_pipe(port, pos);
2621 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2625 /* No active pipe found */
2626 port->busy_grinders--;
2628 grinder->state = e_GRINDER_PREFETCH_PIPE;
2633 rte_panic("Algorithmic error (invalid state)\n");
2639 rte_sched_port_time_resync(struct rte_sched_port *port)
2641 uint64_t cycles = rte_get_tsc_cycles();
2642 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2643 uint64_t bytes_diff;
2645 /* Compute elapsed time in bytes */
2646 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2647 port->inv_cycles_per_byte);
2649 /* Advance port time */
2650 port->time_cpu_cycles = cycles;
2651 port->time_cpu_bytes += bytes_diff;
2652 if (port->time < port->time_cpu_bytes)
2653 port->time = port->time_cpu_bytes;
2655 /* Reset pipe loop detection */
2656 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2660 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2664 /* Check if any exception flag is set */
2665 exceptions = (second_pass && port->busy_grinders == 0) ||
2666 (port->pipe_exhaustion == 1);
2668 /* Clear exception flags */
2669 port->pipe_exhaustion = 0;
2675 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2679 port->pkts_out = pkts;
2680 port->n_pkts_out = 0;
2682 rte_sched_port_time_resync(port);
2684 /* Take each queue in the grinder one step further */
2685 for (i = 0, count = 0; ; i++) {
2686 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2687 if ((count == n_pkts) ||
2688 rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {