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_port_get_memory_footprint(struct rte_sched_port_params *params)
556 uint32_t size0, size1;
559 status = rte_sched_port_check_params(params);
561 RTE_LOG(NOTICE, SCHED,
562 "Port scheduler params check failed (%d)\n", status);
567 size0 = sizeof(struct rte_sched_port);
568 size1 = rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_TOTAL);
570 return size0 + size1;
574 rte_sched_port_log_pipe_profile(struct rte_sched_port *port, uint32_t i)
576 struct rte_sched_pipe_profile *p = port->pipe_profiles + i;
578 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
579 " Token bucket: period = %u, credits per period = %u, size = %u\n"
580 " Traffic classes: period = %u,\n"
581 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
582 " Best-effort traffic class oversubscription: weight = %hhu\n"
583 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
588 p->tb_credits_per_period,
591 /* Traffic classes */
593 p->tc_credits_per_period[0],
594 p->tc_credits_per_period[1],
595 p->tc_credits_per_period[2],
596 p->tc_credits_per_period[3],
597 p->tc_credits_per_period[4],
598 p->tc_credits_per_period[5],
599 p->tc_credits_per_period[6],
600 p->tc_credits_per_period[7],
601 p->tc_credits_per_period[8],
602 p->tc_credits_per_period[9],
603 p->tc_credits_per_period[10],
604 p->tc_credits_per_period[11],
605 p->tc_credits_per_period[12],
607 /* Best-effort traffic class oversubscription */
611 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
614 static inline uint64_t
615 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
617 uint64_t time = time_ms;
619 time = (time * rate) / 1000;
625 rte_sched_pipe_profile_convert(struct rte_sched_port *port,
626 struct rte_sched_pipe_params *src,
627 struct rte_sched_pipe_profile *dst,
630 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
631 uint32_t lcd1, lcd2, lcd;
635 if (src->tb_rate == rate) {
636 dst->tb_credits_per_period = 1;
639 double tb_rate = (double) src->tb_rate
641 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
643 rte_approx(tb_rate, d,
644 &dst->tb_credits_per_period, &dst->tb_period);
647 dst->tb_size = src->tb_size;
649 /* Traffic Classes */
650 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
653 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
655 dst->tc_credits_per_period[i]
656 = rte_sched_time_ms_to_bytes(src->tc_period,
659 dst->tc_ov_weight = src->tc_ov_weight;
662 wrr_cost[0] = src->wrr_weights[0];
663 wrr_cost[1] = src->wrr_weights[1];
664 wrr_cost[2] = src->wrr_weights[2];
665 wrr_cost[3] = src->wrr_weights[3];
667 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
668 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
669 lcd = rte_get_lcd(lcd1, lcd2);
671 wrr_cost[0] = lcd / wrr_cost[0];
672 wrr_cost[1] = lcd / wrr_cost[1];
673 wrr_cost[2] = lcd / wrr_cost[2];
674 wrr_cost[3] = lcd / wrr_cost[3];
676 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
677 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
678 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
679 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
682 struct rte_sched_port *
683 rte_sched_port_config(struct rte_sched_port_params *params)
685 struct rte_sched_port *port = NULL;
686 uint32_t size0, size1;
687 uint32_t cycles_per_byte;
691 status = rte_sched_port_check_params(params);
694 "%s: Port scheduler params check failed (%d)\n",
699 size0 = sizeof(struct rte_sched_port);
700 size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
702 /* Allocate memory to store the data structures */
703 port = rte_zmalloc_socket("qos_params", size0 + size1, RTE_CACHE_LINE_SIZE,
706 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
711 /* User parameters */
712 port->n_subports_per_port = params->n_subports_per_port;
713 port->n_pipes_per_subport = params->n_pipes_per_subport;
714 port->n_pipes_per_subport_log2 =
715 __builtin_ctz(params->n_pipes_per_subport);
716 port->socket = params->socket;
718 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
719 port->pipe_queue[i] = i;
721 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
722 port->pipe_tc[i] = j;
724 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
728 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
729 port->tc_queue[i] = j;
731 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
734 port->rate = params->rate;
735 port->mtu = params->mtu + params->frame_overhead;
736 port->frame_overhead = params->frame_overhead;
739 port->time_cpu_cycles = rte_get_tsc_cycles();
740 port->time_cpu_bytes = 0;
743 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
745 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
748 port->pkts_out = NULL;
749 port->n_pkts_out = 0;
755 rte_sched_subport_free(struct rte_sched_port *port,
756 struct rte_sched_subport *subport)
758 uint32_t n_subport_pipe_queues;
764 n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
766 /* Free enqueued mbufs */
767 for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
768 struct rte_mbuf **mbufs =
769 rte_sched_subport_pipe_qbase(subport, qindex);
770 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
772 struct rte_sched_queue *queue = subport->queue + qindex;
773 uint16_t qr = queue->qr & (qsize - 1);
774 uint16_t qw = queue->qw & (qsize - 1);
776 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
777 rte_pktmbuf_free(mbufs[qr]);
781 rte_bitmap_free(subport->bmp);
785 rte_sched_port_free(struct rte_sched_port *port)
789 /* Check user parameters */
793 for (i = 0; i < port->n_subports_per_port; i++)
794 rte_sched_subport_free(port, port->subports[i]);
800 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
802 struct rte_sched_subport *s = port->subport + i;
804 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
805 " Token bucket: period = %u, credits per period = %u, size = %u\n"
806 " Traffic classes: period = %u\n"
807 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
808 " Best effort traffic class oversubscription: wm min = %u, wm max = %u\n",
813 s->tb_credits_per_period,
816 /* Traffic classes */
818 s->tc_credits_per_period[0],
819 s->tc_credits_per_period[1],
820 s->tc_credits_per_period[2],
821 s->tc_credits_per_period[3],
822 s->tc_credits_per_period[4],
823 s->tc_credits_per_period[5],
824 s->tc_credits_per_period[6],
825 s->tc_credits_per_period[7],
826 s->tc_credits_per_period[8],
827 s->tc_credits_per_period[9],
828 s->tc_credits_per_period[10],
829 s->tc_credits_per_period[11],
830 s->tc_credits_per_period[12],
832 /* Best effort traffic class oversubscription */
838 rte_sched_subport_config(struct rte_sched_port *port,
840 struct rte_sched_subport_params *params)
842 struct rte_sched_subport *s;
845 /* Check user parameters */
848 "%s: Incorrect value for parameter port\n", __func__);
852 if (subport_id >= port->n_subports_per_port) {
854 "%s: Incorrect value for subport id\n", __func__);
858 if (params == NULL) {
860 "%s: Incorrect value for parameter params\n", __func__);
864 if (params->tb_rate == 0 || params->tb_rate > port->rate) {
866 "%s: Incorrect value for tb rate\n", __func__);
870 if (params->tb_size == 0) {
872 "%s: Incorrect value for tb size\n", __func__);
876 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
877 uint32_t tc_rate = params->tc_rate[i];
878 uint16_t qsize = port->qsize[i];
880 if ((qsize == 0 && tc_rate != 0) ||
881 (qsize != 0 && tc_rate == 0) ||
882 (tc_rate > params->tb_rate)) {
884 "%s: Incorrect value for tc rate\n", __func__);
889 if (port->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
890 params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
892 "%s: Incorrect value for tc rate(best effort)\n", __func__);
896 if (params->tc_period == 0) {
898 "%s: Incorrect value for tc period\n", __func__);
902 s = port->subport + subport_id;
904 /* Token Bucket (TB) */
905 if (params->tb_rate == port->rate) {
906 s->tb_credits_per_period = 1;
909 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
910 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
912 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
915 s->tb_size = params->tb_size;
916 s->tb_time = port->time;
917 s->tb_credits = s->tb_size / 2;
919 /* Traffic Classes (TCs) */
920 s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
921 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
923 s->tc_credits_per_period[i]
924 = rte_sched_time_ms_to_bytes(params->tc_period,
928 s->tc_time = port->time + s->tc_period;
929 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
931 s->tc_credits[i] = s->tc_credits_per_period[i];
933 /* TC oversubscription */
934 s->tc_ov_wm_min = port->mtu;
935 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
936 port->pipe_tc_be_rate_max);
937 s->tc_ov_wm = s->tc_ov_wm_max;
938 s->tc_ov_period_id = 0;
943 rte_sched_port_log_subport_config(port, subport_id);
949 rte_sched_pipe_config(struct rte_sched_port *port,
952 int32_t pipe_profile)
954 struct rte_sched_subport *s;
955 struct rte_sched_pipe *p;
956 struct rte_sched_pipe_profile *params;
957 uint32_t deactivate, profile, i;
959 /* Check user parameters */
960 profile = (uint32_t) pipe_profile;
961 deactivate = (pipe_profile < 0);
965 "%s: Incorrect value for parameter port\n", __func__);
969 if (subport_id >= port->n_subports_per_port) {
971 "%s: Incorrect value for parameter subport id\n", __func__);
975 if (pipe_id >= port->n_pipes_per_subport) {
977 "%s: Incorrect value for parameter pipe id\n", __func__);
981 if (!deactivate && profile >= port->n_pipe_profiles) {
983 "%s: Incorrect value for parameter pipe profile\n", __func__);
987 /* Check that subport configuration is valid */
988 s = port->subport + subport_id;
989 if (s->tb_period == 0) {
991 "%s: Subport configuration invalid\n", __func__);
995 p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
997 /* Handle the case when pipe already has a valid configuration */
999 params = port->pipe_profiles + p->profile;
1001 double subport_tc_be_rate =
1002 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1003 / (double) s->tc_period;
1004 double pipe_tc_be_rate =
1005 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1006 / (double) params->tc_period;
1007 uint32_t tc_be_ov = s->tc_ov;
1009 /* Unplug pipe from its subport */
1010 s->tc_ov_n -= params->tc_ov_weight;
1011 s->tc_ov_rate -= pipe_tc_be_rate;
1012 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1014 if (s->tc_ov != tc_be_ov) {
1015 RTE_LOG(DEBUG, SCHED,
1016 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1017 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1020 /* Reset the pipe */
1021 memset(p, 0, sizeof(struct rte_sched_pipe));
1027 /* Apply the new pipe configuration */
1028 p->profile = profile;
1029 params = port->pipe_profiles + p->profile;
1031 /* Token Bucket (TB) */
1032 p->tb_time = port->time;
1033 p->tb_credits = params->tb_size / 2;
1035 /* Traffic Classes (TCs) */
1036 p->tc_time = port->time + params->tc_period;
1038 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1040 p->tc_credits[i] = params->tc_credits_per_period[i];
1043 /* Subport best effort tc oversubscription */
1044 double subport_tc_be_rate =
1045 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1046 / (double) s->tc_period;
1047 double pipe_tc_be_rate =
1048 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1049 / (double) params->tc_period;
1050 uint32_t tc_be_ov = s->tc_ov;
1052 s->tc_ov_n += params->tc_ov_weight;
1053 s->tc_ov_rate += pipe_tc_be_rate;
1054 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1056 if (s->tc_ov != tc_be_ov) {
1057 RTE_LOG(DEBUG, SCHED,
1058 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1059 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1061 p->tc_ov_period_id = s->tc_ov_period_id;
1062 p->tc_ov_credits = s->tc_ov_wm;
1069 rte_sched_port_pipe_profile_add(struct rte_sched_port *port,
1070 struct rte_sched_pipe_params *params,
1071 uint32_t *pipe_profile_id)
1073 struct rte_sched_pipe_profile *pp;
1080 "%s: Incorrect value for parameter port\n", __func__);
1084 /* Pipe profiles not exceeds the max limit */
1085 if (port->n_pipe_profiles >= port->n_max_pipe_profiles) {
1087 "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1092 status = pipe_profile_check(params, port->rate, &port->qsize[0]);
1095 "%s: Pipe profile check failed(%d)\n", __func__, status);
1099 pp = &port->pipe_profiles[port->n_pipe_profiles];
1100 rte_sched_pipe_profile_convert(port, params, pp, port->rate);
1102 /* Pipe profile not exists */
1103 for (i = 0; i < port->n_pipe_profiles; i++)
1104 if (memcmp(port->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1106 "%s: Pipe profile doesn't exist\n", __func__);
1110 /* Pipe profile commit */
1111 *pipe_profile_id = port->n_pipe_profiles;
1112 port->n_pipe_profiles++;
1114 if (port->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1115 port->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1117 rte_sched_port_log_pipe_profile(port, *pipe_profile_id);
1122 static inline uint32_t
1123 rte_sched_port_qindex(struct rte_sched_port *port,
1126 uint32_t traffic_class,
1129 return ((subport & (port->n_subports_per_port - 1)) <<
1130 (port->n_pipes_per_subport_log2 + 4)) |
1131 ((pipe & (port->n_pipes_per_subport - 1)) << 4) |
1132 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1133 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1137 rte_sched_port_pkt_write(struct rte_sched_port *port,
1138 struct rte_mbuf *pkt,
1139 uint32_t subport, uint32_t pipe,
1140 uint32_t traffic_class,
1141 uint32_t queue, enum rte_color color)
1144 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1146 rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1150 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1151 const struct rte_mbuf *pkt,
1152 uint32_t *subport, uint32_t *pipe,
1153 uint32_t *traffic_class, uint32_t *queue)
1155 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1157 *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1158 *pipe = (queue_id >> 4) & (port->n_pipes_per_subport - 1);
1159 *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1160 *queue = rte_sched_port_tc_queue(port, queue_id);
1164 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1166 return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1170 rte_sched_subport_read_stats(struct rte_sched_port *port,
1171 uint32_t subport_id,
1172 struct rte_sched_subport_stats *stats,
1175 struct rte_sched_subport *s;
1177 /* Check user parameters */
1180 "%s: Incorrect value for parameter port\n", __func__);
1184 if (subport_id >= port->n_subports_per_port) {
1186 "%s: Incorrect value for subport id\n", __func__);
1190 if (stats == NULL) {
1192 "%s: Incorrect value for parameter stats\n", __func__);
1196 if (tc_ov == NULL) {
1198 "%s: Incorrect value for tc_ov\n", __func__);
1202 s = port->subport + subport_id;
1204 /* Copy subport stats and clear */
1205 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1206 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1208 /* Subport TC oversubscription status */
1215 rte_sched_queue_read_stats(struct rte_sched_port *port,
1217 struct rte_sched_queue_stats *stats,
1220 struct rte_sched_queue *q;
1221 struct rte_sched_queue_extra *qe;
1223 /* Check user parameters */
1226 "%s: Incorrect value for parameter port\n", __func__);
1230 if (queue_id >= rte_sched_port_queues_per_port(port)) {
1232 "%s: Incorrect value for queue id\n", __func__);
1236 if (stats == NULL) {
1238 "%s: Incorrect value for parameter stats\n", __func__);
1244 "%s: Incorrect value for parameter qlen\n", __func__);
1247 q = port->queue + queue_id;
1248 qe = port->queue_extra + queue_id;
1250 /* Copy queue stats and clear */
1251 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1252 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1255 *qlen = q->qw - q->qr;
1260 #ifdef RTE_SCHED_DEBUG
1263 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1265 struct rte_sched_queue *queue = port->queue + qindex;
1267 return queue->qr == queue->qw;
1270 #endif /* RTE_SCHED_DEBUG */
1272 #ifdef RTE_SCHED_COLLECT_STATS
1275 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1277 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1278 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1279 uint32_t pkt_len = pkt->pkt_len;
1281 s->stats.n_pkts_tc[tc_index] += 1;
1282 s->stats.n_bytes_tc[tc_index] += pkt_len;
1285 #ifdef RTE_SCHED_RED
1287 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1289 struct rte_mbuf *pkt, uint32_t red)
1292 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1294 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1297 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1298 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1299 uint32_t pkt_len = pkt->pkt_len;
1301 s->stats.n_pkts_tc_dropped[tc_index] += 1;
1302 s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1303 #ifdef RTE_SCHED_RED
1304 s->stats.n_pkts_red_dropped[tc_index] += red;
1309 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1311 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1312 uint32_t pkt_len = pkt->pkt_len;
1314 qe->stats.n_pkts += 1;
1315 qe->stats.n_bytes += pkt_len;
1318 #ifdef RTE_SCHED_RED
1320 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1322 struct rte_mbuf *pkt, uint32_t red)
1325 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1327 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1330 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1331 uint32_t pkt_len = pkt->pkt_len;
1333 qe->stats.n_pkts_dropped += 1;
1334 qe->stats.n_bytes_dropped += pkt_len;
1335 #ifdef RTE_SCHED_RED
1336 qe->stats.n_pkts_red_dropped += red;
1340 #endif /* RTE_SCHED_COLLECT_STATS */
1342 #ifdef RTE_SCHED_RED
1345 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1347 struct rte_sched_queue_extra *qe;
1348 struct rte_red_config *red_cfg;
1349 struct rte_red *red;
1351 enum rte_color color;
1353 tc_index = rte_sched_port_pipe_tc(port, qindex);
1354 color = rte_sched_port_pkt_read_color(pkt);
1355 red_cfg = &port->red_config[tc_index][color];
1357 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1360 qe = port->queue_extra + qindex;
1363 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1367 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1369 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1370 struct rte_red *red = &qe->red;
1372 rte_red_mark_queue_empty(red, port->time);
1377 #define rte_sched_port_red_drop(port, pkt, qindex, qlen) 0
1379 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1381 #endif /* RTE_SCHED_RED */
1383 #ifdef RTE_SCHED_DEBUG
1386 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1393 rte_panic("Empty slab at position %u\n", bmp_pos);
1396 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1397 if (mask & bmp_slab) {
1398 if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1399 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1406 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1410 #endif /* RTE_SCHED_DEBUG */
1412 static inline uint32_t
1413 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port,
1414 struct rte_mbuf *pkt)
1416 struct rte_sched_queue *q;
1417 #ifdef RTE_SCHED_COLLECT_STATS
1418 struct rte_sched_queue_extra *qe;
1420 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1422 q = port->queue + qindex;
1424 #ifdef RTE_SCHED_COLLECT_STATS
1425 qe = port->queue_extra + qindex;
1433 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1434 uint32_t qindex, struct rte_mbuf **qbase)
1436 struct rte_sched_queue *q;
1437 struct rte_mbuf **q_qw;
1440 q = port->queue + qindex;
1441 qsize = rte_sched_port_qsize(port, qindex);
1442 q_qw = qbase + (q->qw & (qsize - 1));
1444 rte_prefetch0(q_qw);
1445 rte_bitmap_prefetch0(port->bmp, qindex);
1449 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex,
1450 struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1452 struct rte_sched_queue *q;
1456 q = port->queue + qindex;
1457 qsize = rte_sched_port_qsize(port, qindex);
1458 qlen = q->qw - q->qr;
1460 /* Drop the packet (and update drop stats) when queue is full */
1461 if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) ||
1463 rte_pktmbuf_free(pkt);
1464 #ifdef RTE_SCHED_COLLECT_STATS
1465 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt,
1467 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt,
1473 /* Enqueue packet */
1474 qbase[q->qw & (qsize - 1)] = pkt;
1477 /* Activate queue in the port bitmap */
1478 rte_bitmap_set(port->bmp, qindex);
1481 #ifdef RTE_SCHED_COLLECT_STATS
1482 rte_sched_port_update_subport_stats(port, qindex, pkt);
1483 rte_sched_port_update_queue_stats(port, qindex, pkt);
1491 * The enqueue function implements a 4-level pipeline with each stage
1492 * processing two different packets. The purpose of using a pipeline
1493 * is to hide the latency of prefetching the data structures. The
1494 * naming convention is presented in the diagram below:
1496 * p00 _______ p10 _______ p20 _______ p30 _______
1497 * ----->| |----->| |----->| |----->| |----->
1498 * | 0 | | 1 | | 2 | | 3 |
1499 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1504 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1507 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1508 *pkt30, *pkt31, *pkt_last;
1509 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1510 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1511 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1512 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1518 * Less then 6 input packets available, which is not enough to
1521 if (unlikely(n_pkts < 6)) {
1522 struct rte_mbuf **q_base[5];
1525 /* Prefetch the mbuf structure of each packet */
1526 for (i = 0; i < n_pkts; i++)
1527 rte_prefetch0(pkts[i]);
1529 /* Prefetch the queue structure for each queue */
1530 for (i = 0; i < n_pkts; i++)
1531 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port,
1534 /* Prefetch the write pointer location of each queue */
1535 for (i = 0; i < n_pkts; i++) {
1536 q_base[i] = rte_sched_port_qbase(port, q[i]);
1537 rte_sched_port_enqueue_qwa_prefetch0(port, q[i],
1541 /* Write each packet to its queue */
1542 for (i = 0; i < n_pkts; i++)
1543 result += rte_sched_port_enqueue_qwa(port, q[i],
1544 q_base[i], pkts[i]);
1549 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1552 rte_prefetch0(pkt20);
1553 rte_prefetch0(pkt21);
1557 rte_prefetch0(pkt10);
1558 rte_prefetch0(pkt11);
1560 q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1561 q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1565 rte_prefetch0(pkt00);
1566 rte_prefetch0(pkt01);
1568 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1569 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1571 q20_base = rte_sched_port_qbase(port, q20);
1572 q21_base = rte_sched_port_qbase(port, q21);
1573 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1574 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1576 /* Run the pipeline */
1577 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1578 /* Propagate stage inputs */
1589 q30_base = q20_base;
1590 q31_base = q21_base;
1592 /* Stage 0: Get packets in */
1594 pkt01 = pkts[i + 1];
1595 rte_prefetch0(pkt00);
1596 rte_prefetch0(pkt01);
1598 /* Stage 1: Prefetch queue structure storing queue pointers */
1599 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1600 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1602 /* Stage 2: Prefetch queue write location */
1603 q20_base = rte_sched_port_qbase(port, q20);
1604 q21_base = rte_sched_port_qbase(port, q21);
1605 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1606 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1608 /* Stage 3: Write packet to queue and activate queue */
1609 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1610 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1611 result += r30 + r31;
1615 * Drain the pipeline (exactly 6 packets).
1616 * Handle the last packet in the case
1617 * of an odd number of input packets.
1619 pkt_last = pkts[n_pkts - 1];
1620 rte_prefetch0(pkt_last);
1622 q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1623 q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1625 q10_base = rte_sched_port_qbase(port, q10);
1626 q11_base = rte_sched_port_qbase(port, q11);
1627 rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1628 rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1630 r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1631 r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1632 result += r20 + r21;
1634 q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1636 q00_base = rte_sched_port_qbase(port, q00);
1637 q01_base = rte_sched_port_qbase(port, q01);
1638 rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1639 rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1641 r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1642 r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1643 result += r10 + r11;
1645 q_last_base = rte_sched_port_qbase(port, q_last);
1646 rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1648 r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1649 r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1650 result += r00 + r01;
1653 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1660 #ifndef RTE_SCHED_SUBPORT_TC_OV
1663 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1665 struct rte_sched_grinder *grinder = port->grinder + pos;
1666 struct rte_sched_subport *subport = grinder->subport;
1667 struct rte_sched_pipe *pipe = grinder->pipe;
1668 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1673 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1674 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1675 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1676 subport->tb_time += n_periods * subport->tb_period;
1679 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1680 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1681 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1682 pipe->tb_time += n_periods * params->tb_period;
1685 if (unlikely(port->time >= subport->tc_time)) {
1686 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1687 subport->tc_credits[i] = subport->tc_credits_per_period[i];
1689 subport->tc_time = port->time + subport->tc_period;
1693 if (unlikely(port->time >= pipe->tc_time)) {
1694 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1695 pipe->tc_credits[i] = params->tc_credits_per_period[i];
1697 pipe->tc_time = port->time + params->tc_period;
1703 static inline uint32_t
1704 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1706 struct rte_sched_grinder *grinder = port->grinder + pos;
1707 struct rte_sched_subport *subport = grinder->subport;
1708 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1709 uint32_t tc_consumption = 0, tc_ov_consumption_max;
1710 uint32_t tc_ov_wm = subport->tc_ov_wm;
1713 if (subport->tc_ov == 0)
1714 return subport->tc_ov_wm_max;
1716 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
1717 tc_ov_consumption[i] =
1718 subport->tc_credits_per_period[i] - subport->tc_credits[i];
1719 tc_consumption += tc_ov_consumption[i];
1722 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
1723 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
1724 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
1726 tc_ov_consumption_max =
1727 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
1730 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
1731 (tc_ov_consumption_max - port->mtu)) {
1732 tc_ov_wm -= tc_ov_wm >> 7;
1733 if (tc_ov_wm < subport->tc_ov_wm_min)
1734 tc_ov_wm = subport->tc_ov_wm_min;
1739 tc_ov_wm += (tc_ov_wm >> 7) + 1;
1740 if (tc_ov_wm > subport->tc_ov_wm_max)
1741 tc_ov_wm = subport->tc_ov_wm_max;
1747 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1749 struct rte_sched_grinder *grinder = port->grinder + pos;
1750 struct rte_sched_subport *subport = grinder->subport;
1751 struct rte_sched_pipe *pipe = grinder->pipe;
1752 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1757 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1758 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1759 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1760 subport->tb_time += n_periods * subport->tb_period;
1763 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1764 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1765 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1766 pipe->tb_time += n_periods * params->tb_period;
1769 if (unlikely(port->time >= subport->tc_time)) {
1770 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
1772 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1773 subport->tc_credits[i] = subport->tc_credits_per_period[i];
1775 subport->tc_time = port->time + subport->tc_period;
1776 subport->tc_ov_period_id++;
1780 if (unlikely(port->time >= pipe->tc_time)) {
1781 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1782 pipe->tc_credits[i] = params->tc_credits_per_period[i];
1783 pipe->tc_time = port->time + params->tc_period;
1786 /* Pipe TCs - Oversubscription */
1787 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
1788 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
1790 pipe->tc_ov_period_id = subport->tc_ov_period_id;
1794 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
1797 #ifndef RTE_SCHED_SUBPORT_TC_OV
1800 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1802 struct rte_sched_grinder *grinder = port->grinder + pos;
1803 struct rte_sched_subport *subport = grinder->subport;
1804 struct rte_sched_pipe *pipe = grinder->pipe;
1805 struct rte_mbuf *pkt = grinder->pkt;
1806 uint32_t tc_index = grinder->tc_index;
1807 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1808 uint32_t subport_tb_credits = subport->tb_credits;
1809 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1810 uint32_t pipe_tb_credits = pipe->tb_credits;
1811 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1814 /* Check queue credits */
1815 enough_credits = (pkt_len <= subport_tb_credits) &&
1816 (pkt_len <= subport_tc_credits) &&
1817 (pkt_len <= pipe_tb_credits) &&
1818 (pkt_len <= pipe_tc_credits);
1820 if (!enough_credits)
1823 /* Update port credits */
1824 subport->tb_credits -= pkt_len;
1825 subport->tc_credits[tc_index] -= pkt_len;
1826 pipe->tb_credits -= pkt_len;
1827 pipe->tc_credits[tc_index] -= pkt_len;
1835 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1837 struct rte_sched_grinder *grinder = port->grinder + pos;
1838 struct rte_sched_subport *subport = grinder->subport;
1839 struct rte_sched_pipe *pipe = grinder->pipe;
1840 struct rte_mbuf *pkt = grinder->pkt;
1841 uint32_t tc_index = grinder->tc_index;
1842 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1843 uint32_t subport_tb_credits = subport->tb_credits;
1844 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1845 uint32_t pipe_tb_credits = pipe->tb_credits;
1846 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1847 uint32_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1848 uint32_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
1849 uint32_t pipe_tc_ov_credits, i;
1852 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1853 pipe_tc_ov_mask1[i] = UINT32_MAX;
1855 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
1856 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = UINT32_MAX;
1857 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
1859 /* Check pipe and subport credits */
1860 enough_credits = (pkt_len <= subport_tb_credits) &&
1861 (pkt_len <= subport_tc_credits) &&
1862 (pkt_len <= pipe_tb_credits) &&
1863 (pkt_len <= pipe_tc_credits) &&
1864 (pkt_len <= pipe_tc_ov_credits);
1866 if (!enough_credits)
1869 /* Update pipe and subport credits */
1870 subport->tb_credits -= pkt_len;
1871 subport->tc_credits[tc_index] -= pkt_len;
1872 pipe->tb_credits -= pkt_len;
1873 pipe->tc_credits[tc_index] -= pkt_len;
1874 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
1879 #endif /* RTE_SCHED_SUBPORT_TC_OV */
1883 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
1885 struct rte_sched_grinder *grinder = port->grinder + pos;
1886 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
1887 struct rte_mbuf *pkt = grinder->pkt;
1888 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1889 uint32_t be_tc_active;
1891 if (!grinder_credits_check(port, pos))
1894 /* Advance port time */
1895 port->time += pkt_len;
1898 port->pkts_out[port->n_pkts_out++] = pkt;
1901 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
1902 grinder->wrr_tokens[grinder->qpos] +=
1903 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
1905 if (queue->qr == queue->qw) {
1906 uint32_t qindex = grinder->qindex[grinder->qpos];
1908 rte_bitmap_clear(port->bmp, qindex);
1909 grinder->qmask &= ~(1 << grinder->qpos);
1911 grinder->wrr_mask[grinder->qpos] = 0;
1912 rte_sched_port_set_queue_empty_timestamp(port, qindex);
1915 /* Reset pipe loop detection */
1916 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1917 grinder->productive = 1;
1922 #ifdef SCHED_VECTOR_SSE4
1925 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1927 __m128i index = _mm_set1_epi32(base_pipe);
1928 __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
1929 __m128i res = _mm_cmpeq_epi32(pipes, index);
1931 pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
1932 pipes = _mm_cmpeq_epi32(pipes, index);
1933 res = _mm_or_si128(res, pipes);
1935 if (_mm_testz_si128(res, res))
1941 #elif defined(SCHED_VECTOR_NEON)
1944 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1946 uint32x4_t index, pipes;
1947 uint32_t *pos = (uint32_t *)port->grinder_base_bmp_pos;
1949 index = vmovq_n_u32(base_pipe);
1950 pipes = vld1q_u32(pos);
1951 if (!vminvq_u32(veorq_u32(pipes, index)))
1954 pipes = vld1q_u32(pos + 4);
1955 if (!vminvq_u32(veorq_u32(pipes, index)))
1964 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1968 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
1969 if (port->grinder_base_bmp_pos[i] == base_pipe)
1976 #endif /* RTE_SCHED_OPTIMIZATIONS */
1979 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
1981 struct rte_sched_grinder *grinder = port->grinder + pos;
1984 grinder->pcache_w = 0;
1985 grinder->pcache_r = 0;
1987 w[0] = (uint16_t) bmp_slab;
1988 w[1] = (uint16_t) (bmp_slab >> 16);
1989 w[2] = (uint16_t) (bmp_slab >> 32);
1990 w[3] = (uint16_t) (bmp_slab >> 48);
1992 grinder->pcache_qmask[grinder->pcache_w] = w[0];
1993 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
1994 grinder->pcache_w += (w[0] != 0);
1996 grinder->pcache_qmask[grinder->pcache_w] = w[1];
1997 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
1998 grinder->pcache_w += (w[1] != 0);
2000 grinder->pcache_qmask[grinder->pcache_w] = w[2];
2001 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2002 grinder->pcache_w += (w[2] != 0);
2004 grinder->pcache_qmask[grinder->pcache_w] = w[3];
2005 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2006 grinder->pcache_w += (w[3] != 0);
2010 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
2012 struct rte_sched_grinder *grinder = port->grinder + pos;
2015 grinder->tccache_w = 0;
2016 grinder->tccache_r = 0;
2018 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2019 b = (uint8_t) ((qmask >> i) & 0x1);
2020 grinder->tccache_qmask[grinder->tccache_w] = b;
2021 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2022 grinder->tccache_w += (b != 0);
2025 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2026 grinder->tccache_qmask[grinder->tccache_w] = b;
2027 grinder->tccache_qindex[grinder->tccache_w] = qindex +
2028 RTE_SCHED_TRAFFIC_CLASS_BE;
2029 grinder->tccache_w += (b != 0);
2033 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
2035 struct rte_sched_grinder *grinder = port->grinder + pos;
2036 struct rte_mbuf **qbase;
2040 if (grinder->tccache_r == grinder->tccache_w)
2043 qindex = grinder->tccache_qindex[grinder->tccache_r];
2044 qbase = rte_sched_port_qbase(port, qindex);
2045 qsize = rte_sched_port_qsize(port, qindex);
2047 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2048 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2049 grinder->qsize = qsize;
2051 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2052 grinder->queue[0] = port->queue + qindex;
2053 grinder->qbase[0] = qbase;
2054 grinder->qindex[0] = qindex;
2055 grinder->tccache_r++;
2060 grinder->queue[0] = port->queue + qindex;
2061 grinder->queue[1] = port->queue + qindex + 1;
2062 grinder->queue[2] = port->queue + qindex + 2;
2063 grinder->queue[3] = port->queue + qindex + 3;
2065 grinder->qbase[0] = qbase;
2066 grinder->qbase[1] = qbase + qsize;
2067 grinder->qbase[2] = qbase + 2 * qsize;
2068 grinder->qbase[3] = qbase + 3 * qsize;
2070 grinder->qindex[0] = qindex;
2071 grinder->qindex[1] = qindex + 1;
2072 grinder->qindex[2] = qindex + 2;
2073 grinder->qindex[3] = qindex + 3;
2075 grinder->tccache_r++;
2080 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
2082 struct rte_sched_grinder *grinder = port->grinder + pos;
2083 uint32_t pipe_qindex;
2084 uint16_t pipe_qmask;
2086 if (grinder->pcache_r < grinder->pcache_w) {
2087 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2088 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2089 grinder->pcache_r++;
2091 uint64_t bmp_slab = 0;
2092 uint32_t bmp_pos = 0;
2094 /* Get another non-empty pipe group */
2095 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
2098 #ifdef RTE_SCHED_DEBUG
2099 debug_check_queue_slab(port, bmp_pos, bmp_slab);
2102 /* Return if pipe group already in one of the other grinders */
2103 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2104 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
2107 port->grinder_base_bmp_pos[pos] = bmp_pos;
2109 /* Install new pipe group into grinder's pipe cache */
2110 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
2112 pipe_qmask = grinder->pcache_qmask[0];
2113 pipe_qindex = grinder->pcache_qindex[0];
2114 grinder->pcache_r = 1;
2117 /* Install new pipe in the grinder */
2118 grinder->pindex = pipe_qindex >> 4;
2119 grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
2120 grinder->pipe = port->pipe + grinder->pindex;
2121 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2122 grinder->productive = 0;
2124 grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
2125 grinder_next_tc(port, pos);
2127 /* Check for pipe exhaustion */
2128 if (grinder->pindex == port->pipe_loop) {
2129 port->pipe_exhaustion = 1;
2130 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2138 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
2140 struct rte_sched_grinder *grinder = port->grinder + pos;
2141 struct rte_sched_pipe *pipe = grinder->pipe;
2142 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2143 uint32_t qmask = grinder->qmask;
2145 grinder->wrr_tokens[0] =
2146 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2147 grinder->wrr_tokens[1] =
2148 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2149 grinder->wrr_tokens[2] =
2150 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2151 grinder->wrr_tokens[3] =
2152 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2154 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2155 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2156 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2157 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2159 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2160 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2161 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2162 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2166 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
2168 struct rte_sched_grinder *grinder = port->grinder + pos;
2169 struct rte_sched_pipe *pipe = grinder->pipe;
2171 pipe->wrr_tokens[0] =
2172 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2173 RTE_SCHED_WRR_SHIFT;
2174 pipe->wrr_tokens[1] =
2175 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2176 RTE_SCHED_WRR_SHIFT;
2177 pipe->wrr_tokens[2] =
2178 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2179 RTE_SCHED_WRR_SHIFT;
2180 pipe->wrr_tokens[3] =
2181 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2182 RTE_SCHED_WRR_SHIFT;
2186 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
2188 struct rte_sched_grinder *grinder = port->grinder + pos;
2189 uint16_t wrr_tokens_min;
2191 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2192 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2193 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2194 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2196 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2197 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2199 grinder->wrr_tokens[0] -= wrr_tokens_min;
2200 grinder->wrr_tokens[1] -= wrr_tokens_min;
2201 grinder->wrr_tokens[2] -= wrr_tokens_min;
2202 grinder->wrr_tokens[3] -= wrr_tokens_min;
2206 #define grinder_evict(port, pos)
2209 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
2211 struct rte_sched_grinder *grinder = port->grinder + pos;
2213 rte_prefetch0(grinder->pipe);
2214 rte_prefetch0(grinder->queue[0]);
2218 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
2220 struct rte_sched_grinder *grinder = port->grinder + pos;
2221 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2223 qsize = grinder->qsize;
2226 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2227 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2229 rte_prefetch0(grinder->qbase[0] + qr[0]);
2233 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2234 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2235 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2236 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2238 rte_prefetch0(grinder->qbase[0] + qr[0]);
2239 rte_prefetch0(grinder->qbase[1] + qr[1]);
2241 grinder_wrr_load(port, pos);
2242 grinder_wrr(port, pos);
2244 rte_prefetch0(grinder->qbase[2] + qr[2]);
2245 rte_prefetch0(grinder->qbase[3] + qr[3]);
2249 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2251 struct rte_sched_grinder *grinder = port->grinder + pos;
2252 uint32_t qpos = grinder->qpos;
2253 struct rte_mbuf **qbase = grinder->qbase[qpos];
2254 uint16_t qsize = grinder->qsize;
2255 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2257 grinder->pkt = qbase[qr];
2258 rte_prefetch0(grinder->pkt);
2260 if (unlikely((qr & 0x7) == 7)) {
2261 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2263 rte_prefetch0(qbase + qr_next);
2267 static inline uint32_t
2268 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2270 struct rte_sched_grinder *grinder = port->grinder + pos;
2272 switch (grinder->state) {
2273 case e_GRINDER_PREFETCH_PIPE:
2275 if (grinder_next_pipe(port, pos)) {
2276 grinder_prefetch_pipe(port, pos);
2277 port->busy_grinders++;
2279 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2286 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2288 struct rte_sched_pipe *pipe = grinder->pipe;
2290 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2291 grinder_prefetch_tc_queue_arrays(port, pos);
2292 grinder_credits_update(port, pos);
2294 grinder->state = e_GRINDER_PREFETCH_MBUF;
2298 case e_GRINDER_PREFETCH_MBUF:
2300 grinder_prefetch_mbuf(port, pos);
2302 grinder->state = e_GRINDER_READ_MBUF;
2306 case e_GRINDER_READ_MBUF:
2308 uint32_t wrr_active, result = 0;
2310 result = grinder_schedule(port, pos);
2312 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2314 /* Look for next packet within the same TC */
2315 if (result && grinder->qmask) {
2317 grinder_wrr(port, pos);
2319 grinder_prefetch_mbuf(port, pos);
2325 grinder_wrr_store(port, pos);
2327 /* Look for another active TC within same pipe */
2328 if (grinder_next_tc(port, pos)) {
2329 grinder_prefetch_tc_queue_arrays(port, pos);
2331 grinder->state = e_GRINDER_PREFETCH_MBUF;
2335 if (grinder->productive == 0 &&
2336 port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2337 port->pipe_loop = grinder->pindex;
2339 grinder_evict(port, pos);
2341 /* Look for another active pipe */
2342 if (grinder_next_pipe(port, pos)) {
2343 grinder_prefetch_pipe(port, pos);
2345 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2349 /* No active pipe found */
2350 port->busy_grinders--;
2352 grinder->state = e_GRINDER_PREFETCH_PIPE;
2357 rte_panic("Algorithmic error (invalid state)\n");
2363 rte_sched_port_time_resync(struct rte_sched_port *port)
2365 uint64_t cycles = rte_get_tsc_cycles();
2366 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2367 uint64_t bytes_diff;
2369 /* Compute elapsed time in bytes */
2370 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2371 port->inv_cycles_per_byte);
2373 /* Advance port time */
2374 port->time_cpu_cycles = cycles;
2375 port->time_cpu_bytes += bytes_diff;
2376 if (port->time < port->time_cpu_bytes)
2377 port->time = port->time_cpu_bytes;
2379 /* Reset pipe loop detection */
2380 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2384 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2388 /* Check if any exception flag is set */
2389 exceptions = (second_pass && port->busy_grinders == 0) ||
2390 (port->pipe_exhaustion == 1);
2392 /* Clear exception flags */
2393 port->pipe_exhaustion = 0;
2399 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2403 port->pkts_out = pkts;
2404 port->n_pkts_out = 0;
2406 rte_sched_port_time_resync(port);
2408 /* Take each queue in the grinder one step further */
2409 for (i = 0, count = 0; ; i++) {
2410 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2411 if ((count == n_pkts) ||
2412 rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {