4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_common.h>
39 #include <rte_memory.h>
40 #include <rte_malloc.h>
41 #include <rte_cycles.h>
42 #include <rte_prefetch.h>
43 #include <rte_branch_prediction.h>
46 #include "rte_sched.h"
47 #include "rte_bitmap.h"
48 #include "rte_sched_common.h"
49 #include "rte_approx.h"
51 #ifdef __INTEL_COMPILER
52 #pragma warning(disable:2259) /* conversion may lose significant bits */
55 #ifndef RTE_SCHED_DEBUG
56 #define RTE_SCHED_DEBUG 0
59 #ifndef RTE_SCHED_OPTIMIZATIONS
60 #define RTE_SCHED_OPTIMIZATIONS 0
63 #if RTE_SCHED_OPTIMIZATIONS
64 #include <immintrin.h>
67 #define RTE_SCHED_ENQUEUE 1
69 #define RTE_SCHED_TS 1
71 #if RTE_SCHED_TS == 0 /* Infinite credits. Traffic shaping disabled. */
72 #define RTE_SCHED_TS_CREDITS_UPDATE 0
73 #define RTE_SCHED_TS_CREDITS_CHECK 0
74 #else /* Real Credits. Full traffic shaping implemented. */
75 #define RTE_SCHED_TS_CREDITS_UPDATE 1
76 #define RTE_SCHED_TS_CREDITS_CHECK 1
79 #ifndef RTE_SCHED_TB_RATE_CONFIG_ERR
80 #define RTE_SCHED_TB_RATE_CONFIG_ERR (1e-7)
83 #define RTE_SCHED_WRR 1
85 #ifndef RTE_SCHED_WRR_SHIFT
86 #define RTE_SCHED_WRR_SHIFT 3
89 #ifndef RTE_SCHED_PORT_N_GRINDERS
90 #define RTE_SCHED_PORT_N_GRINDERS 8
92 #if (RTE_SCHED_PORT_N_GRINDERS == 0) || (RTE_SCHED_PORT_N_GRINDERS & (RTE_SCHED_PORT_N_GRINDERS - 1))
93 #error Number of grinders must be non-zero and a power of 2
95 #if (RTE_SCHED_OPTIMIZATIONS && (RTE_SCHED_PORT_N_GRINDERS != 8))
96 #error Number of grinders must be 8 when RTE_SCHED_OPTIMIZATIONS is set
99 #define RTE_SCHED_GRINDER_PCACHE_SIZE (64 / RTE_SCHED_QUEUES_PER_PIPE)
101 #define RTE_SCHED_PIPE_INVALID UINT32_MAX
103 #define RTE_SCHED_BMP_POS_INVALID UINT32_MAX
105 struct rte_sched_subport {
106 /* Token bucket (TB) */
107 uint64_t tb_time; /* time of last update */
109 uint32_t tb_credits_per_period;
113 /* Traffic classes (TCs) */
114 uint64_t tc_time; /* time of next update */
115 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
116 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
119 /* TC oversubscription */
121 uint32_t tc_ov_wm_min;
122 uint32_t tc_ov_wm_max;
123 uint8_t tc_ov_period_id;
129 struct rte_sched_subport_stats stats;
132 struct rte_sched_pipe_profile {
133 /* Token bucket (TB) */
135 uint32_t tb_credits_per_period;
138 /* Pipe traffic classes */
140 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
141 uint8_t tc_ov_weight;
144 uint8_t wrr_cost[RTE_SCHED_QUEUES_PER_PIPE];
147 struct rte_sched_pipe {
148 /* Token bucket (TB) */
149 uint64_t tb_time; /* time of last update */
152 /* Pipe profile and flags */
155 /* Traffic classes (TCs) */
156 uint64_t tc_time; /* time of next update */
157 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
159 /* Weighted Round Robin (WRR) */
160 uint8_t wrr_tokens[RTE_SCHED_QUEUES_PER_PIPE];
162 /* TC oversubscription */
163 uint32_t tc_ov_credits;
164 uint8_t tc_ov_period_id;
166 } __rte_cache_aligned;
168 struct rte_sched_queue {
173 struct rte_sched_queue_extra {
174 struct rte_sched_queue_stats stats;
181 e_GRINDER_PREFETCH_PIPE = 0,
182 e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
183 e_GRINDER_PREFETCH_MBUF,
187 struct rte_sched_grinder {
189 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
190 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
195 enum grinder_state state;
198 struct rte_sched_subport *subport;
199 struct rte_sched_pipe *pipe;
200 struct rte_sched_pipe_profile *pipe_params;
203 uint8_t tccache_qmask[4];
204 uint32_t tccache_qindex[4];
210 struct rte_sched_queue *queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
211 struct rte_mbuf **qbase[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
212 uint32_t qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
216 struct rte_mbuf *pkt;
219 uint16_t wrr_tokens[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
220 uint16_t wrr_mask[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
221 uint8_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
224 struct rte_sched_port {
225 /* User parameters */
226 uint32_t n_subports_per_port;
227 uint32_t n_pipes_per_subport;
230 uint32_t frame_overhead;
231 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
232 uint32_t n_pipe_profiles;
233 uint32_t pipe_tc3_rate_max;
235 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][e_RTE_METER_COLORS];
239 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cyles */
240 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
241 uint64_t time; /* Current NIC TX time measured in bytes */
242 double cycles_per_byte; /* CPU cycles per byte */
244 /* Scheduling loop detection */
246 uint32_t pipe_exhaustion;
249 struct rte_bitmap *bmp;
250 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
253 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
254 uint32_t busy_grinders;
255 struct rte_mbuf **pkts_out;
258 /* Queue base calculation */
259 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
262 /* Large data structures */
263 struct rte_sched_subport *subport;
264 struct rte_sched_pipe *pipe;
265 struct rte_sched_queue *queue;
266 struct rte_sched_queue_extra *queue_extra;
267 struct rte_sched_pipe_profile *pipe_profiles;
269 struct rte_mbuf **queue_array;
270 uint8_t memory[0] __rte_cache_aligned;
271 } __rte_cache_aligned;
273 enum rte_sched_port_array {
274 e_RTE_SCHED_PORT_ARRAY_SUBPORT = 0,
275 e_RTE_SCHED_PORT_ARRAY_PIPE,
276 e_RTE_SCHED_PORT_ARRAY_QUEUE,
277 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA,
278 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES,
279 e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY,
280 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY,
281 e_RTE_SCHED_PORT_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_port_queues_per_port(struct rte_sched_port *port)
297 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport * port->n_subports_per_port;
301 rte_sched_port_check_params(struct rte_sched_port_params *params)
305 if (params == NULL) {
310 if ((params->socket < 0) || (params->socket >= RTE_MAX_NUMA_NODES)) {
315 if (params->rate == 0) {
320 if (params->mtu == 0) {
324 /* n_subports_per_port: non-zero, power of 2 */
325 if ((params->n_subports_per_port == 0) || (!rte_is_power_of_2(params->n_subports_per_port))) {
329 /* n_pipes_per_subport: non-zero, power of 2 */
330 if ((params->n_pipes_per_subport == 0) || (!rte_is_power_of_2(params->n_pipes_per_subport))) {
334 /* qsize: non-zero, power of 2, no bigger than 32K (due to 16-bit read/write pointers) */
335 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
336 uint16_t qsize = params->qsize[i];
338 if ((qsize == 0) || (!rte_is_power_of_2(qsize))) {
343 /* pipe_profiles and n_pipe_profiles */
344 if ((params->pipe_profiles == NULL) ||
345 (params->n_pipe_profiles == 0) ||
346 (params->n_pipe_profiles > RTE_SCHED_PIPE_PROFILES_PER_PORT)) {
350 for (i = 0; i < params->n_pipe_profiles; i ++) {
351 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
353 /* TB rate: non-zero, not greater than port rate */
354 if ((p->tb_rate == 0) || (p->tb_rate > params->rate)) {
358 /* TB size: non-zero */
359 if (p->tb_size == 0) {
363 /* TC rate: non-zero, less than pipe rate */
364 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j ++) {
365 if ((p->tc_rate[j] == 0) || (p->tc_rate[j] > p->tb_rate)) {
370 /* TC period: non-zero */
371 if (p->tc_period == 0) {
375 #ifdef RTE_SCHED_SUBPORT_TC_OV
376 /* TC3 oversubscription weight: non-zero */
377 if (p->tc_ov_weight == 0) {
382 /* Queue WRR weights: non-zero */
383 for (j = 0; j < RTE_SCHED_QUEUES_PER_PIPE; j ++) {
384 if (p->wrr_weights[j] == 0) {
394 rte_sched_port_get_array_base(struct rte_sched_port_params *params, enum rte_sched_port_array array)
396 uint32_t n_subports_per_port = params->n_subports_per_port;
397 uint32_t n_pipes_per_subport = params->n_pipes_per_subport;
398 uint32_t n_pipes_per_port = n_pipes_per_subport * n_subports_per_port;
399 uint32_t n_queues_per_port = RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport * n_subports_per_port;
401 uint32_t size_subport = n_subports_per_port * sizeof(struct rte_sched_subport);
402 uint32_t size_pipe = n_pipes_per_port * sizeof(struct rte_sched_pipe);
403 uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue);
404 uint32_t size_queue_extra = n_queues_per_port * sizeof(struct rte_sched_queue_extra);
405 uint32_t size_pipe_profiles = RTE_SCHED_PIPE_PROFILES_PER_PORT * sizeof(struct rte_sched_pipe_profile);
406 uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port);
407 uint32_t size_per_pipe_queue_array, size_queue_array;
411 size_per_pipe_queue_array = 0;
412 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
413 size_per_pipe_queue_array += RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS * params->qsize[i] * sizeof(struct rte_mbuf *);
415 size_queue_array = n_pipes_per_port * size_per_pipe_queue_array;
419 if (array == e_RTE_SCHED_PORT_ARRAY_SUBPORT) return base;
420 base += RTE_CACHE_LINE_ROUNDUP(size_subport);
422 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE) return base;
423 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
425 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE) return base;
426 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
428 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA) return base;
429 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
431 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES) return base;
432 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
434 if (array == e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY) return base;
435 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
437 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY) return base;
438 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
444 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
446 uint32_t size0, size1;
449 status = rte_sched_port_check_params(params);
451 RTE_LOG(NOTICE, SCHED,
452 "Port scheduler params check failed (%d)\n", status);
457 size0 = sizeof(struct rte_sched_port);
458 size1 = rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_TOTAL);
460 return (size0 + size1);
464 rte_sched_port_config_qsize(struct rte_sched_port *port)
467 port->qsize_add[0] = 0;
468 port->qsize_add[1] = port->qsize_add[0] + port->qsize[0];
469 port->qsize_add[2] = port->qsize_add[1] + port->qsize[0];
470 port->qsize_add[3] = port->qsize_add[2] + port->qsize[0];
473 port->qsize_add[4] = port->qsize_add[3] + port->qsize[0];
474 port->qsize_add[5] = port->qsize_add[4] + port->qsize[1];
475 port->qsize_add[6] = port->qsize_add[5] + port->qsize[1];
476 port->qsize_add[7] = port->qsize_add[6] + port->qsize[1];
479 port->qsize_add[8] = port->qsize_add[7] + port->qsize[1];
480 port->qsize_add[9] = port->qsize_add[8] + port->qsize[2];
481 port->qsize_add[10] = port->qsize_add[9] + port->qsize[2];
482 port->qsize_add[11] = port->qsize_add[10] + port->qsize[2];
485 port->qsize_add[12] = port->qsize_add[11] + port->qsize[2];
486 port->qsize_add[13] = port->qsize_add[12] + port->qsize[3];
487 port->qsize_add[14] = port->qsize_add[13] + port->qsize[3];
488 port->qsize_add[15] = port->qsize_add[14] + port->qsize[3];
490 port->qsize_sum = port->qsize_add[15] + port->qsize[3];
494 rte_sched_port_log_pipe_profile(struct rte_sched_port *port, uint32_t i)
496 struct rte_sched_pipe_profile *p = port->pipe_profiles + i;
498 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
499 " Token bucket: period = %u, credits per period = %u, size = %u\n"
500 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
501 " Traffic class 3 oversubscription: weight = %hhu\n"
502 " WRR cost: [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu]\n",
507 p->tb_credits_per_period,
510 /* Traffic classes */
512 p->tc_credits_per_period[0],
513 p->tc_credits_per_period[1],
514 p->tc_credits_per_period[2],
515 p->tc_credits_per_period[3],
517 /* Traffic class 3 oversubscription */
521 p->wrr_cost[ 0], p->wrr_cost[ 1], p->wrr_cost[ 2], p->wrr_cost[ 3],
522 p->wrr_cost[ 4], p->wrr_cost[ 5], p->wrr_cost[ 6], p->wrr_cost[ 7],
523 p->wrr_cost[ 8], p->wrr_cost[ 9], p->wrr_cost[10], p->wrr_cost[11],
524 p->wrr_cost[12], p->wrr_cost[13], p->wrr_cost[14], p->wrr_cost[15]);
527 static inline uint64_t
528 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
530 uint64_t time = time_ms;
531 time = (time * rate) / 1000;
537 rte_sched_port_config_pipe_profile_table(struct rte_sched_port *port, struct rte_sched_port_params *params)
541 for (i = 0; i < port->n_pipe_profiles; i ++) {
542 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
543 struct rte_sched_pipe_profile *dst = port->pipe_profiles + i;
546 if (src->tb_rate == params->rate) {
547 dst->tb_credits_per_period = 1;
550 double tb_rate = ((double) src->tb_rate) / ((double) params->rate);
551 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
553 rte_approx(tb_rate, d, &dst->tb_credits_per_period, &dst->tb_period);
555 dst->tb_size = src->tb_size;
557 /* Traffic Classes */
558 dst->tc_period = (uint32_t) rte_sched_time_ms_to_bytes(src->tc_period, params->rate);
559 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j ++) {
560 dst->tc_credits_per_period[j] = (uint32_t) rte_sched_time_ms_to_bytes(src->tc_period, src->tc_rate[j]);
562 #ifdef RTE_SCHED_SUBPORT_TC_OV
563 dst->tc_ov_weight = src->tc_ov_weight;
567 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j ++) {
568 uint32_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
569 uint32_t lcd, lcd1, lcd2;
572 qindex = j * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS;
574 wrr_cost[0] = src->wrr_weights[qindex];
575 wrr_cost[1] = src->wrr_weights[qindex + 1];
576 wrr_cost[2] = src->wrr_weights[qindex + 2];
577 wrr_cost[3] = src->wrr_weights[qindex + 3];
579 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
580 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
581 lcd = rte_get_lcd(lcd1, lcd2);
583 wrr_cost[0] = lcd / wrr_cost[0];
584 wrr_cost[1] = lcd / wrr_cost[1];
585 wrr_cost[2] = lcd / wrr_cost[2];
586 wrr_cost[3] = lcd / wrr_cost[3];
588 dst->wrr_cost[qindex] = (uint8_t) wrr_cost[0];
589 dst->wrr_cost[qindex + 1] = (uint8_t) wrr_cost[1];
590 dst->wrr_cost[qindex + 2] = (uint8_t) wrr_cost[2];
591 dst->wrr_cost[qindex + 3] = (uint8_t) wrr_cost[3];
594 rte_sched_port_log_pipe_profile(port, i);
597 port->pipe_tc3_rate_max = 0;
598 for (i = 0; i < port->n_pipe_profiles; i ++) {
599 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
600 uint32_t pipe_tc3_rate = src->tc_rate[3];
602 if (port->pipe_tc3_rate_max < pipe_tc3_rate) {
603 port->pipe_tc3_rate_max = pipe_tc3_rate;
608 struct rte_sched_port *
609 rte_sched_port_config(struct rte_sched_port_params *params)
611 struct rte_sched_port *port = NULL;
612 uint32_t mem_size, bmp_mem_size, n_queues_per_port, i;
614 /* Check user parameters. Determine the amount of memory to allocate */
615 mem_size = rte_sched_port_get_memory_footprint(params);
620 /* Allocate memory to store the data structures */
621 port = rte_zmalloc("qos_params", mem_size, RTE_CACHE_LINE_SIZE);
626 /* User parameters */
627 port->n_subports_per_port = params->n_subports_per_port;
628 port->n_pipes_per_subport = params->n_pipes_per_subport;
629 port->rate = params->rate;
630 port->mtu = params->mtu + params->frame_overhead;
631 port->frame_overhead = params->frame_overhead;
632 memcpy(port->qsize, params->qsize, sizeof(params->qsize));
633 port->n_pipe_profiles = params->n_pipe_profiles;
636 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
639 for (j = 0; j < e_RTE_METER_COLORS; j++) {
640 /* if min/max are both zero, then RED is disabled */
641 if ((params->red_params[i][j].min_th |
642 params->red_params[i][j].max_th) == 0) {
646 if (rte_red_config_init(&port->red_config[i][j],
647 params->red_params[i][j].wq_log2,
648 params->red_params[i][j].min_th,
649 params->red_params[i][j].max_th,
650 params->red_params[i][j].maxp_inv) != 0) {
658 port->time_cpu_cycles = rte_get_tsc_cycles();
659 port->time_cpu_bytes = 0;
661 port->cycles_per_byte = ((double) rte_get_tsc_hz()) / ((double) params->rate);
663 /* Scheduling loop detection */
664 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
665 port->pipe_exhaustion = 0;
668 port->busy_grinders = 0;
669 port->pkts_out = NULL;
670 port->n_pkts_out = 0;
672 /* Queue base calculation */
673 rte_sched_port_config_qsize(port);
675 /* Large data structures */
676 port->subport = (struct rte_sched_subport *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_SUBPORT));
677 port->pipe = (struct rte_sched_pipe *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_PIPE));
678 port->queue = (struct rte_sched_queue *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_QUEUE));
679 port->queue_extra = (struct rte_sched_queue_extra *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA));
680 port->pipe_profiles = (struct rte_sched_pipe_profile *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES));
681 port->bmp_array = port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY);
682 port->queue_array = (struct rte_mbuf **) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY));
684 /* Pipe profile table */
685 rte_sched_port_config_pipe_profile_table(port, params);
688 n_queues_per_port = rte_sched_port_queues_per_port(port);
689 bmp_mem_size = rte_bitmap_get_memory_footprint(n_queues_per_port);
690 port->bmp = rte_bitmap_init(n_queues_per_port, port->bmp_array, bmp_mem_size);
691 if (port->bmp == NULL) {
692 RTE_LOG(ERR, SCHED, "Bitmap init error\n");
695 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i ++) {
696 port->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
703 rte_sched_port_free(struct rte_sched_port *port)
705 /* Check user parameters */
710 rte_bitmap_free(port->bmp);
715 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
717 struct rte_sched_subport *s = port->subport + i;
719 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
720 " Token bucket: period = %u, credits per period = %u, size = %u\n"
721 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
722 " Traffic class 3 oversubscription: wm min = %u, wm max = %u\n",
727 s->tb_credits_per_period,
730 /* Traffic classes */
732 s->tc_credits_per_period[0],
733 s->tc_credits_per_period[1],
734 s->tc_credits_per_period[2],
735 s->tc_credits_per_period[3],
737 /* Traffic class 3 oversubscription */
743 rte_sched_subport_config(struct rte_sched_port *port,
745 struct rte_sched_subport_params *params)
747 struct rte_sched_subport *s;
750 /* Check user parameters */
751 if ((port == NULL) ||
752 (subport_id >= port->n_subports_per_port) ||
757 if ((params->tb_rate == 0) || (params->tb_rate > port->rate)) {
761 if (params->tb_size == 0) {
765 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
766 if ((params->tc_rate[i] == 0) || (params->tc_rate[i] > params->tb_rate)) {
771 if (params->tc_period == 0) {
775 s = port->subport + subport_id;
777 /* Token Bucket (TB) */
778 if (params->tb_rate == port->rate) {
779 s->tb_credits_per_period = 1;
782 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
783 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
785 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
787 s->tb_size = params->tb_size;
788 s->tb_time = port->time;
789 s->tb_credits = s->tb_size / 2;
791 /* Traffic Classes (TCs) */
792 s->tc_period = (uint32_t) rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
793 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
794 s->tc_credits_per_period[i] = (uint32_t) rte_sched_time_ms_to_bytes(params->tc_period, params->tc_rate[i]);
796 s->tc_time = port->time + s->tc_period;
797 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
798 s->tc_credits[i] = s->tc_credits_per_period[i];
801 #ifdef RTE_SCHED_SUBPORT_TC_OV
802 /* TC oversubscription */
803 s->tc_ov_wm_min = port->mtu;
804 s->tc_ov_wm_max = (uint32_t) rte_sched_time_ms_to_bytes(params->tc_period, port->pipe_tc3_rate_max);
805 s->tc_ov_wm = s->tc_ov_wm_max;
806 s->tc_ov_period_id = 0;
812 rte_sched_port_log_subport_config(port, subport_id);
818 rte_sched_pipe_config(struct rte_sched_port *port,
821 int32_t pipe_profile)
823 struct rte_sched_subport *s;
824 struct rte_sched_pipe *p;
825 struct rte_sched_pipe_profile *params;
826 uint32_t deactivate, profile, i;
828 /* Check user parameters */
829 profile = (uint32_t) pipe_profile;
830 deactivate = (pipe_profile < 0);
831 if ((port == NULL) ||
832 (subport_id >= port->n_subports_per_port) ||
833 (pipe_id >= port->n_pipes_per_subport) ||
834 ((!deactivate) && (profile >= port->n_pipe_profiles))) {
838 /* Check that subport configuration is valid */
839 s = port->subport + subport_id;
840 if (s->tb_period == 0) {
844 p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
846 /* Handle the case when pipe already has a valid configuration */
848 params = port->pipe_profiles + p->profile;
850 #ifdef RTE_SCHED_SUBPORT_TC_OV
851 double subport_tc3_rate = ((double) s->tc_credits_per_period[3]) / ((double) s->tc_period);
852 double pipe_tc3_rate = ((double) params->tc_credits_per_period[3]) / ((double) params->tc_period);
853 uint32_t tc3_ov = s->tc_ov;
855 /* Unplug pipe from its subport */
856 s->tc_ov_n -= params->tc_ov_weight;
857 s->tc_ov_rate -= pipe_tc3_rate;
858 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
860 if (s->tc_ov != tc3_ov) {
861 RTE_LOG(DEBUG, SCHED,
862 "Subport %u TC3 oversubscription is OFF (%.4lf >= %.4lf)\n",
863 subport_id, subport_tc3_rate, s->tc_ov_rate);
868 memset(p, 0, sizeof(struct rte_sched_pipe));
875 /* Apply the new pipe configuration */
876 p->profile = profile;
877 params = port->pipe_profiles + p->profile;
879 /* Token Bucket (TB) */
880 p->tb_time = port->time;
881 p->tb_credits = params->tb_size / 2;
883 /* Traffic Classes (TCs) */
884 p->tc_time = port->time + params->tc_period;
885 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
886 p->tc_credits[i] = params->tc_credits_per_period[i];
889 #ifdef RTE_SCHED_SUBPORT_TC_OV
891 /* Subport TC3 oversubscription */
892 double subport_tc3_rate = ((double) s->tc_credits_per_period[3]) / ((double) s->tc_period);
893 double pipe_tc3_rate = ((double) params->tc_credits_per_period[3]) / ((double) params->tc_period);
894 uint32_t tc3_ov = s->tc_ov;
896 s->tc_ov_n += params->tc_ov_weight;
897 s->tc_ov_rate += pipe_tc3_rate;
898 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
900 if (s->tc_ov != tc3_ov) {
901 RTE_LOG(DEBUG, SCHED,
902 "Subport %u TC3 oversubscription is ON (%.4lf < %.4lf)\n",
903 subport_id, subport_tc3_rate, s->tc_ov_rate);
905 p->tc_ov_period_id = s->tc_ov_period_id;
906 p->tc_ov_credits = s->tc_ov_wm;
914 rte_sched_subport_read_stats(struct rte_sched_port *port,
916 struct rte_sched_subport_stats *stats,
919 struct rte_sched_subport *s;
921 /* Check user parameters */
922 if ((port == NULL) ||
923 (subport_id >= port->n_subports_per_port) ||
928 s = port->subport + subport_id;
930 /* Copy subport stats and clear */
931 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
932 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
934 /* Subport TC ovesubscription status */
941 rte_sched_queue_read_stats(struct rte_sched_port *port,
943 struct rte_sched_queue_stats *stats,
946 struct rte_sched_queue *q;
947 struct rte_sched_queue_extra *qe;
949 /* Check user parameters */
950 if ((port == NULL) ||
951 (queue_id >= rte_sched_port_queues_per_port(port)) ||
956 q = port->queue + queue_id;
957 qe = port->queue_extra + queue_id;
959 /* Copy queue stats and clear */
960 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
961 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
964 *qlen = q->qw - q->qr;
969 static inline uint32_t
970 rte_sched_port_qindex(struct rte_sched_port *port, uint32_t subport, uint32_t pipe, uint32_t traffic_class, uint32_t queue)
974 result = subport * port->n_pipes_per_subport + pipe;
975 result = result * RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE + traffic_class;
976 result = result * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
981 static inline struct rte_mbuf **
982 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
984 uint32_t pindex = qindex >> 4;
985 uint32_t qpos = qindex & 0xF;
987 return (port->queue_array + pindex * port->qsize_sum + port->qsize_add[qpos]);
990 static inline uint16_t
991 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
993 uint32_t tc = (qindex >> 2) & 0x3;
995 return port->qsize[tc];
1001 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1003 struct rte_sched_queue *queue = port->queue + qindex;
1005 return (queue->qr == queue->qw);
1009 rte_sched_port_queue_is_full(struct rte_sched_port *port, uint32_t qindex)
1011 struct rte_sched_queue *queue = port->queue + qindex;
1012 uint16_t qsize = rte_sched_port_qsize(port, qindex);
1013 uint16_t qlen = queue->qw - queue->qr;
1015 return (qlen >= qsize);
1018 #endif /* RTE_SCHED_DEBUG */
1020 #ifdef RTE_SCHED_COLLECT_STATS
1023 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1025 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1026 uint32_t tc_index = (qindex >> 2) & 0x3;
1027 uint32_t pkt_len = pkt->pkt_len;
1029 s->stats.n_pkts_tc[tc_index] += 1;
1030 s->stats.n_bytes_tc[tc_index] += pkt_len;
1034 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1036 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1037 uint32_t tc_index = (qindex >> 2) & 0x3;
1038 uint32_t pkt_len = pkt->pkt_len;
1040 s->stats.n_pkts_tc_dropped[tc_index] += 1;
1041 s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1045 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1047 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1048 uint32_t pkt_len = pkt->pkt_len;
1050 qe->stats.n_pkts += 1;
1051 qe->stats.n_bytes += pkt_len;
1055 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1057 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1058 uint32_t pkt_len = pkt->pkt_len;
1060 qe->stats.n_pkts_dropped += 1;
1061 qe->stats.n_bytes_dropped += pkt_len;
1064 #endif /* RTE_SCHED_COLLECT_STATS */
1066 #ifdef RTE_SCHED_RED
1069 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1071 struct rte_sched_queue_extra *qe;
1072 struct rte_red_config *red_cfg;
1073 struct rte_red *red;
1075 enum rte_meter_color color;
1077 tc_index = (qindex >> 2) & 0x3;
1078 color = rte_sched_port_pkt_read_color(pkt);
1079 red_cfg = &port->red_config[tc_index][color];
1081 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1084 qe = port->queue_extra + qindex;
1087 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1091 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1093 struct rte_sched_queue_extra *qe;
1094 struct rte_red *red;
1096 qe = port->queue_extra + qindex;
1099 rte_red_mark_queue_empty(red, port->time);
1104 #define rte_sched_port_red_drop(port, pkt, qindex, qlen) 0
1106 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1108 #endif /* RTE_SCHED_RED */
1113 debug_pipe_is_empty(struct rte_sched_port *port, uint32_t pindex)
1117 qindex = pindex << 4;
1119 for (i = 0; i < 16; i ++){
1120 uint32_t queue_empty = rte_sched_port_queue_is_empty(port, qindex + i);
1121 uint32_t bmp_bit_clear = (rte_bitmap_get(port->bmp, qindex + i) == 0);
1123 if (queue_empty != bmp_bit_clear){
1124 rte_panic("Queue status mismatch for queue %u of pipe %u\n", i, pindex);
1136 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos, uint64_t bmp_slab)
1142 rte_panic("Empty slab at position %u\n", bmp_pos);
1146 for (i = 0, mask = 1; i < 64; i ++, mask <<= 1) {
1147 if (mask & bmp_slab){
1148 if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1149 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1156 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1161 #endif /* RTE_SCHED_DEBUG */
1163 static inline uint32_t
1164 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port, struct rte_mbuf *pkt)
1166 struct rte_sched_queue *q;
1167 #ifdef RTE_SCHED_COLLECT_STATS
1168 struct rte_sched_queue_extra *qe;
1170 uint32_t subport, pipe, traffic_class, queue, qindex;
1172 rte_sched_port_pkt_read_tree_path(pkt, &subport, &pipe, &traffic_class, &queue);
1174 qindex = rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1175 q = port->queue + qindex;
1177 #ifdef RTE_SCHED_COLLECT_STATS
1178 qe = port->queue_extra + qindex;
1186 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf **qbase)
1188 struct rte_sched_queue *q;
1189 struct rte_mbuf **q_qw;
1192 q = port->queue + qindex;
1193 qsize = rte_sched_port_qsize(port, qindex);
1194 q_qw = qbase + (q->qw & (qsize - 1));
1196 rte_prefetch0(q_qw);
1197 rte_bitmap_prefetch0(port->bmp, qindex);
1201 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1203 struct rte_sched_queue *q;
1207 q = port->queue + qindex;
1208 qsize = rte_sched_port_qsize(port, qindex);
1209 qlen = q->qw - q->qr;
1211 /* Drop the packet (and update drop stats) when queue is full */
1212 if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) || (qlen >= qsize))) {
1213 rte_pktmbuf_free(pkt);
1214 #ifdef RTE_SCHED_COLLECT_STATS
1215 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt);
1216 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt);
1221 /* Enqueue packet */
1222 qbase[q->qw & (qsize - 1)] = pkt;
1225 /* Activate queue in the port bitmap */
1226 rte_bitmap_set(port->bmp, qindex);
1229 #ifdef RTE_SCHED_COLLECT_STATS
1230 rte_sched_port_update_subport_stats(port, qindex, pkt);
1231 rte_sched_port_update_queue_stats(port, qindex, pkt);
1237 #if RTE_SCHED_ENQUEUE == 0
1240 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
1246 for (i = 0; i < n_pkts; i ++) {
1247 struct rte_mbuf *pkt;
1248 struct rte_mbuf **q_base;
1249 uint32_t subport, pipe, traffic_class, queue, qindex;
1253 rte_sched_port_pkt_read_tree_path(pkt, &subport, &pipe, &traffic_class, &queue);
1255 qindex = rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1257 q_base = rte_sched_port_qbase(port, qindex);
1259 result += rte_sched_port_enqueue_qwa(port, qindex, q_base, pkt);
1267 /* The enqueue function implements a 4-level pipeline with each stage processing
1268 * two different packets. The purpose of using a pipeline is to hide the latency
1269 * of prefetching the data structures. The naming convention is presented in the
1272 * p00 _______ p10 _______ p20 _______ p30 _______
1273 * ----->| |----->| |----->| |----->| |----->
1274 * | 0 | | 1 | | 2 | | 3 |
1275 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1280 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
1282 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21, *pkt30, *pkt31, *pkt_last;
1283 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base, **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1284 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1285 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1290 /* Less then 6 input packets available, which is not enough to feed the pipeline */
1291 if (unlikely(n_pkts < 6)) {
1292 struct rte_mbuf **q_base[5];
1295 /* Prefetch the mbuf structure of each packet */
1296 for (i = 0; i < n_pkts; i ++) {
1297 rte_prefetch0(pkts[i]);
1300 /* Prefetch the queue structure for each queue */
1301 for (i = 0; i < n_pkts; i ++) {
1302 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port, pkts[i]);
1305 /* Prefetch the write pointer location of each queue */
1306 for (i = 0; i < n_pkts; i ++) {
1307 q_base[i] = rte_sched_port_qbase(port, q[i]);
1308 rte_sched_port_enqueue_qwa_prefetch0(port, q[i], q_base[i]);
1311 /* Write each packet to its queue */
1312 for (i = 0; i < n_pkts; i ++) {
1313 result += rte_sched_port_enqueue_qwa(port, q[i], q_base[i], pkts[i]);
1319 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1322 rte_prefetch0(pkt20);
1323 rte_prefetch0(pkt21);
1327 rte_prefetch0(pkt10);
1328 rte_prefetch0(pkt11);
1330 q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1331 q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1335 rte_prefetch0(pkt00);
1336 rte_prefetch0(pkt01);
1338 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1339 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1341 q20_base = rte_sched_port_qbase(port, q20);
1342 q21_base = rte_sched_port_qbase(port, q21);
1343 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1344 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1346 /* Run the pipeline */
1347 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1348 /* Propagate stage inputs */
1359 q30_base = q20_base;
1360 q31_base = q21_base;
1362 /* Stage 0: Get packets in */
1364 pkt01 = pkts[i + 1];
1365 rte_prefetch0(pkt00);
1366 rte_prefetch0(pkt01);
1368 /* Stage 1: Prefetch queue structure storing queue pointers */
1369 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1370 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1372 /* Stage 2: Prefetch queue write location */
1373 q20_base = rte_sched_port_qbase(port, q20);
1374 q21_base = rte_sched_port_qbase(port, q21);
1375 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1376 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1378 /* Stage 3: Write packet to queue and activate queue */
1379 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1380 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1381 result += r30 + r31;
1384 /* Drain the pipeline (exactly 6 packets). Handle the last packet in the case
1385 of an odd number of input packets. */
1386 pkt_last = pkts[n_pkts - 1];
1387 rte_prefetch0(pkt_last);
1389 q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1390 q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1392 q10_base = rte_sched_port_qbase(port, q10);
1393 q11_base = rte_sched_port_qbase(port, q11);
1394 rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1395 rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1397 r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1398 r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1399 result += r20 + r21;
1401 q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1403 q00_base = rte_sched_port_qbase(port, q00);
1404 q01_base = rte_sched_port_qbase(port, q01);
1405 rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1406 rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1408 r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1409 r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1410 result += r10 + r11;
1412 q_last_base = rte_sched_port_qbase(port, q_last);
1413 rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1415 r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1416 r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1417 result += r00 + r01;
1420 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1427 #endif /* RTE_SCHED_ENQUEUE */
1429 #if RTE_SCHED_TS_CREDITS_UPDATE == 0
1431 #define grinder_credits_update(port, pos)
1433 #elif !defined(RTE_SCHED_SUBPORT_TC_OV)
1436 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1438 struct rte_sched_grinder *grinder = port->grinder + pos;
1439 struct rte_sched_subport *subport = grinder->subport;
1440 struct rte_sched_pipe *pipe = grinder->pipe;
1441 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1445 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1446 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1447 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1448 subport->tb_time += n_periods * subport->tb_period;
1451 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1452 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1453 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1454 pipe->tb_time += n_periods * params->tb_period;
1457 if (unlikely(port->time >= subport->tc_time)) {
1458 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1459 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1460 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1461 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1462 subport->tc_time = port->time + subport->tc_period;
1466 if (unlikely(port->time >= pipe->tc_time)) {
1467 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1468 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1469 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1470 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1471 pipe->tc_time = port->time + params->tc_period;
1477 static inline uint32_t
1478 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1480 struct rte_sched_grinder *grinder = port->grinder + pos;
1481 struct rte_sched_subport *subport = grinder->subport;
1482 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1483 uint32_t tc_ov_consumption_max;
1484 uint32_t tc_ov_wm = subport->tc_ov_wm;
1486 if (subport->tc_ov == 0) {
1487 return subport->tc_ov_wm_max;
1490 tc_ov_consumption[0] = subport->tc_credits_per_period[0] - subport->tc_credits[0];
1491 tc_ov_consumption[1] = subport->tc_credits_per_period[1] - subport->tc_credits[1];
1492 tc_ov_consumption[2] = subport->tc_credits_per_period[2] - subport->tc_credits[2];
1493 tc_ov_consumption[3] = subport->tc_credits_per_period[3] - subport->tc_credits[3];
1495 tc_ov_consumption_max = subport->tc_credits_per_period[3] -
1496 (tc_ov_consumption[0] + tc_ov_consumption[1] + tc_ov_consumption[2]);
1498 if (tc_ov_consumption[3] > (tc_ov_consumption_max - port->mtu)) {
1499 tc_ov_wm -= tc_ov_wm >> 7;
1500 if (tc_ov_wm < subport->tc_ov_wm_min) {
1501 tc_ov_wm = subport->tc_ov_wm_min;
1506 tc_ov_wm += (tc_ov_wm >> 7) + 1;
1507 if (tc_ov_wm > subport->tc_ov_wm_max) {
1508 tc_ov_wm = subport->tc_ov_wm_max;
1514 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1516 struct rte_sched_grinder *grinder = port->grinder + pos;
1517 struct rte_sched_subport *subport = grinder->subport;
1518 struct rte_sched_pipe *pipe = grinder->pipe;
1519 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1523 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1524 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1525 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1526 subport->tb_time += n_periods * subport->tb_period;
1529 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1530 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1531 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1532 pipe->tb_time += n_periods * params->tb_period;
1535 if (unlikely(port->time >= subport->tc_time)) {
1536 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
1538 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1539 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1540 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1541 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1543 subport->tc_time = port->time + subport->tc_period;
1544 subport->tc_ov_period_id ++;
1548 if (unlikely(port->time >= pipe->tc_time)) {
1549 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1550 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1551 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1552 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1553 pipe->tc_time = port->time + params->tc_period;
1556 /* Pipe TCs - Oversubscription */
1557 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
1558 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
1560 pipe->tc_ov_period_id = subport->tc_ov_period_id;
1564 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
1566 #if RTE_SCHED_TS_CREDITS_CHECK
1568 #ifndef RTE_SCHED_SUBPORT_TC_OV
1571 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1573 struct rte_sched_grinder *grinder = port->grinder + pos;
1574 struct rte_sched_subport *subport = grinder->subport;
1575 struct rte_sched_pipe *pipe = grinder->pipe;
1576 struct rte_mbuf *pkt = grinder->pkt;
1577 uint32_t tc_index = grinder->tc_index;
1578 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1579 uint32_t subport_tb_credits = subport->tb_credits;
1580 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1581 uint32_t pipe_tb_credits = pipe->tb_credits;
1582 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1585 /* Check queue credits */
1586 enough_credits = (pkt_len <= subport_tb_credits) &&
1587 (pkt_len <= subport_tc_credits) &&
1588 (pkt_len <= pipe_tb_credits) &&
1589 (pkt_len <= pipe_tc_credits);
1591 if (!enough_credits) {
1595 /* Update port credits */
1596 subport->tb_credits -= pkt_len;
1597 subport->tc_credits[tc_index] -= pkt_len;
1598 pipe->tb_credits -= pkt_len;
1599 pipe->tc_credits[tc_index] -= pkt_len;
1607 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1609 struct rte_sched_grinder *grinder = port->grinder + pos;
1610 struct rte_sched_subport *subport = grinder->subport;
1611 struct rte_sched_pipe *pipe = grinder->pipe;
1612 struct rte_mbuf *pkt = grinder->pkt;
1613 uint32_t tc_index = grinder->tc_index;
1614 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1615 uint32_t subport_tb_credits = subport->tb_credits;
1616 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1617 uint32_t pipe_tb_credits = pipe->tb_credits;
1618 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1619 uint32_t pipe_tc_ov_mask1[] = {UINT32_MAX, UINT32_MAX, UINT32_MAX, pipe->tc_ov_credits};
1620 uint32_t pipe_tc_ov_mask2[] = {0, 0, 0, UINT32_MAX};
1621 uint32_t pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
1624 /* Check pipe and subport credits */
1625 enough_credits = (pkt_len <= subport_tb_credits) &&
1626 (pkt_len <= subport_tc_credits) &&
1627 (pkt_len <= pipe_tb_credits) &&
1628 (pkt_len <= pipe_tc_credits) &&
1629 (pkt_len <= pipe_tc_ov_credits);
1631 if (!enough_credits) {
1635 /* Update pipe and subport credits */
1636 subport->tb_credits -= pkt_len;
1637 subport->tc_credits[tc_index] -= pkt_len;
1638 pipe->tb_credits -= pkt_len;
1639 pipe->tc_credits[tc_index] -= pkt_len;
1640 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
1645 #endif /* RTE_SCHED_SUBPORT_TC_OV */
1647 #endif /* RTE_SCHED_TS_CREDITS_CHECK */
1650 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
1652 struct rte_sched_grinder *grinder = port->grinder + pos;
1653 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
1654 struct rte_mbuf *pkt = grinder->pkt;
1655 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1657 #if RTE_SCHED_TS_CREDITS_CHECK
1658 if (!grinder_credits_check(port, pos)) {
1663 /* Advance port time */
1664 port->time += pkt_len;
1667 port->pkts_out[port->n_pkts_out ++] = pkt;
1669 grinder->wrr_tokens[grinder->qpos] += pkt_len * grinder->wrr_cost[grinder->qpos];
1670 if (queue->qr == queue->qw) {
1671 uint32_t qindex = grinder->qindex[grinder->qpos];
1673 rte_bitmap_clear(port->bmp, qindex);
1674 grinder->qmask &= ~(1 << grinder->qpos);
1675 grinder->wrr_mask[grinder->qpos] = 0;
1676 rte_sched_port_set_queue_empty_timestamp(port, qindex);
1679 /* Reset pipe loop detection */
1680 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1681 grinder->productive = 1;
1686 #if RTE_SCHED_OPTIMIZATIONS
1689 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1691 __m128i index = _mm_set1_epi32 (base_pipe);
1692 __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
1693 __m128i res = _mm_cmpeq_epi32(pipes, index);
1694 pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
1695 pipes = _mm_cmpeq_epi32(pipes, index);
1696 res = _mm_or_si128(res, pipes);
1698 if (_mm_testz_si128(res, res))
1707 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1711 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i ++) {
1712 if (port->grinder_base_bmp_pos[i] == base_pipe) {
1720 #endif /* RTE_SCHED_OPTIMIZATIONS */
1723 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
1725 struct rte_sched_grinder *grinder = port->grinder + pos;
1728 grinder->pcache_w = 0;
1729 grinder->pcache_r = 0;
1731 w[0] = (uint16_t) bmp_slab;
1732 w[1] = (uint16_t) (bmp_slab >> 16);
1733 w[2] = (uint16_t) (bmp_slab >> 32);
1734 w[3] = (uint16_t) (bmp_slab >> 48);
1736 grinder->pcache_qmask[grinder->pcache_w] = w[0];
1737 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
1738 grinder->pcache_w += (w[0] != 0);
1740 grinder->pcache_qmask[grinder->pcache_w] = w[1];
1741 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
1742 grinder->pcache_w += (w[1] != 0);
1744 grinder->pcache_qmask[grinder->pcache_w] = w[2];
1745 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
1746 grinder->pcache_w += (w[2] != 0);
1748 grinder->pcache_qmask[grinder->pcache_w] = w[3];
1749 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
1750 grinder->pcache_w += (w[3] != 0);
1754 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
1756 struct rte_sched_grinder *grinder = port->grinder + pos;
1759 grinder->tccache_w = 0;
1760 grinder->tccache_r = 0;
1762 b[0] = (uint8_t) (qmask & 0xF);
1763 b[1] = (uint8_t) ((qmask >> 4) & 0xF);
1764 b[2] = (uint8_t) ((qmask >> 8) & 0xF);
1765 b[3] = (uint8_t) ((qmask >> 12) & 0xF);
1767 grinder->tccache_qmask[grinder->tccache_w] = b[0];
1768 grinder->tccache_qindex[grinder->tccache_w] = qindex;
1769 grinder->tccache_w += (b[0] != 0);
1771 grinder->tccache_qmask[grinder->tccache_w] = b[1];
1772 grinder->tccache_qindex[grinder->tccache_w] = qindex + 4;
1773 grinder->tccache_w += (b[1] != 0);
1775 grinder->tccache_qmask[grinder->tccache_w] = b[2];
1776 grinder->tccache_qindex[grinder->tccache_w] = qindex + 8;
1777 grinder->tccache_w += (b[2] != 0);
1779 grinder->tccache_qmask[grinder->tccache_w] = b[3];
1780 grinder->tccache_qindex[grinder->tccache_w] = qindex + 12;
1781 grinder->tccache_w += (b[3] != 0);
1785 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
1787 struct rte_sched_grinder *grinder = port->grinder + pos;
1788 struct rte_mbuf **qbase;
1792 if (grinder->tccache_r == grinder->tccache_w) {
1796 qindex = grinder->tccache_qindex[grinder->tccache_r];
1797 qbase = rte_sched_port_qbase(port, qindex);
1798 qsize = rte_sched_port_qsize(port, qindex);
1800 grinder->tc_index = (qindex >> 2) & 0x3;
1801 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
1802 grinder->qsize = qsize;
1804 grinder->qindex[0] = qindex;
1805 grinder->qindex[1] = qindex + 1;
1806 grinder->qindex[2] = qindex + 2;
1807 grinder->qindex[3] = qindex + 3;
1809 grinder->queue[0] = port->queue + qindex;
1810 grinder->queue[1] = port->queue + qindex + 1;
1811 grinder->queue[2] = port->queue + qindex + 2;
1812 grinder->queue[3] = port->queue + qindex + 3;
1814 grinder->qbase[0] = qbase;
1815 grinder->qbase[1] = qbase + qsize;
1816 grinder->qbase[2] = qbase + 2 * qsize;
1817 grinder->qbase[3] = qbase + 3 * qsize;
1819 grinder->tccache_r ++;
1824 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
1826 struct rte_sched_grinder *grinder = port->grinder + pos;
1827 uint32_t pipe_qindex;
1828 uint16_t pipe_qmask;
1830 if (grinder->pcache_r < grinder->pcache_w) {
1831 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
1832 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
1833 grinder->pcache_r ++;
1835 uint64_t bmp_slab = 0;
1836 uint32_t bmp_pos = 0;
1838 /* Get another non-empty pipe group */
1839 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0)) {
1844 debug_check_queue_slab(port, bmp_pos, bmp_slab);
1847 /* Return if pipe group already in one of the other grinders */
1848 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
1849 if (unlikely(grinder_pipe_exists(port, bmp_pos))) {
1852 port->grinder_base_bmp_pos[pos] = bmp_pos;
1854 /* Install new pipe group into grinder's pipe cache */
1855 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
1857 pipe_qmask = grinder->pcache_qmask[0];
1858 pipe_qindex = grinder->pcache_qindex[0];
1859 grinder->pcache_r = 1;
1862 /* Install new pipe in the grinder */
1863 grinder->pindex = pipe_qindex >> 4;
1864 grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
1865 grinder->pipe = port->pipe + grinder->pindex;
1866 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
1867 grinder->productive = 0;
1869 grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
1870 grinder_next_tc(port, pos);
1872 /* Check for pipe exhaustion */
1873 if (grinder->pindex == port->pipe_loop) {
1874 port->pipe_exhaustion = 1;
1875 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1881 #if RTE_SCHED_WRR == 0
1883 #define grinder_wrr_load(a,b)
1885 #define grinder_wrr_store(a,b)
1888 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
1890 struct rte_sched_grinder *grinder = port->grinder + pos;
1891 uint64_t slab = grinder->qmask;
1893 if (rte_bsf64(slab, &grinder->qpos) == 0) {
1894 rte_panic("grinder wrr\n");
1898 #elif RTE_SCHED_WRR == 1
1901 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
1903 struct rte_sched_grinder *grinder = port->grinder + pos;
1904 struct rte_sched_pipe *pipe = grinder->pipe;
1905 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
1906 uint32_t tc_index = grinder->tc_index;
1907 uint32_t qmask = grinder->qmask;
1910 qindex = tc_index * 4;
1912 grinder->wrr_tokens[0] = ((uint16_t) pipe->wrr_tokens[qindex]) << RTE_SCHED_WRR_SHIFT;
1913 grinder->wrr_tokens[1] = ((uint16_t) pipe->wrr_tokens[qindex + 1]) << RTE_SCHED_WRR_SHIFT;
1914 grinder->wrr_tokens[2] = ((uint16_t) pipe->wrr_tokens[qindex + 2]) << RTE_SCHED_WRR_SHIFT;
1915 grinder->wrr_tokens[3] = ((uint16_t) pipe->wrr_tokens[qindex + 3]) << RTE_SCHED_WRR_SHIFT;
1917 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
1918 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
1919 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
1920 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
1922 grinder->wrr_cost[0] = pipe_params->wrr_cost[qindex];
1923 grinder->wrr_cost[1] = pipe_params->wrr_cost[qindex + 1];
1924 grinder->wrr_cost[2] = pipe_params->wrr_cost[qindex + 2];
1925 grinder->wrr_cost[3] = pipe_params->wrr_cost[qindex + 3];
1929 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
1931 struct rte_sched_grinder *grinder = port->grinder + pos;
1932 struct rte_sched_pipe *pipe = grinder->pipe;
1933 uint32_t tc_index = grinder->tc_index;
1936 qindex = tc_index * 4;
1938 pipe->wrr_tokens[qindex] = (uint8_t) ((grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >> RTE_SCHED_WRR_SHIFT);
1939 pipe->wrr_tokens[qindex + 1] = (uint8_t) ((grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >> RTE_SCHED_WRR_SHIFT);
1940 pipe->wrr_tokens[qindex + 2] = (uint8_t) ((grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >> RTE_SCHED_WRR_SHIFT);
1941 pipe->wrr_tokens[qindex + 3] = (uint8_t) ((grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >> RTE_SCHED_WRR_SHIFT);
1945 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
1947 struct rte_sched_grinder *grinder = port->grinder + pos;
1948 uint16_t wrr_tokens_min;
1950 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
1951 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
1952 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
1953 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
1955 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
1956 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
1958 grinder->wrr_tokens[0] -= wrr_tokens_min;
1959 grinder->wrr_tokens[1] -= wrr_tokens_min;
1960 grinder->wrr_tokens[2] -= wrr_tokens_min;
1961 grinder->wrr_tokens[3] -= wrr_tokens_min;
1966 #error Invalid value for RTE_SCHED_WRR
1968 #endif /* RTE_SCHED_WRR */
1970 #define grinder_evict(port, pos)
1973 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
1975 struct rte_sched_grinder *grinder = port->grinder + pos;
1977 rte_prefetch0(grinder->pipe);
1978 rte_prefetch0(grinder->queue[0]);
1982 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
1984 struct rte_sched_grinder *grinder = port->grinder + pos;
1985 uint16_t qsize, qr[4];
1987 qsize = grinder->qsize;
1988 qr[0] = grinder->queue[0]->qr & (qsize - 1);
1989 qr[1] = grinder->queue[1]->qr & (qsize - 1);
1990 qr[2] = grinder->queue[2]->qr & (qsize - 1);
1991 qr[3] = grinder->queue[3]->qr & (qsize - 1);
1993 rte_prefetch0(grinder->qbase[0] + qr[0]);
1994 rte_prefetch0(grinder->qbase[1] + qr[1]);
1996 grinder_wrr_load(port, pos);
1997 grinder_wrr(port, pos);
1999 rte_prefetch0(grinder->qbase[2] + qr[2]);
2000 rte_prefetch0(grinder->qbase[3] + qr[3]);
2004 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2006 struct rte_sched_grinder *grinder = port->grinder + pos;
2007 uint32_t qpos = grinder->qpos;
2008 struct rte_mbuf **qbase = grinder->qbase[qpos];
2009 uint16_t qsize = grinder->qsize;
2010 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2012 grinder->pkt = qbase[qr];
2013 rte_prefetch0(grinder->pkt);
2015 if (unlikely((qr & 0x7) == 7)) {
2016 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2018 rte_prefetch0(qbase + qr_next);
2022 static inline uint32_t
2023 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2025 struct rte_sched_grinder *grinder = port->grinder + pos;
2027 switch (grinder->state) {
2028 case e_GRINDER_PREFETCH_PIPE:
2030 if (grinder_next_pipe(port, pos)) {
2031 grinder_prefetch_pipe(port, pos);
2032 port->busy_grinders ++;
2034 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2041 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2043 struct rte_sched_pipe *pipe = grinder->pipe;
2045 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2046 grinder_prefetch_tc_queue_arrays(port, pos);
2047 grinder_credits_update(port, pos);
2049 grinder->state = e_GRINDER_PREFETCH_MBUF;
2053 case e_GRINDER_PREFETCH_MBUF:
2055 grinder_prefetch_mbuf(port, pos);
2057 grinder->state = e_GRINDER_READ_MBUF;
2061 case e_GRINDER_READ_MBUF:
2063 uint32_t result = 0;
2065 result = grinder_schedule(port, pos);
2067 /* Look for next packet within the same TC */
2068 if (result && grinder->qmask) {
2069 grinder_wrr(port, pos);
2070 grinder_prefetch_mbuf(port, pos);
2074 grinder_wrr_store(port, pos);
2076 /* Look for another active TC within same pipe */
2077 if (grinder_next_tc(port, pos)) {
2078 grinder_prefetch_tc_queue_arrays(port, pos);
2080 grinder->state = e_GRINDER_PREFETCH_MBUF;
2083 if ((grinder->productive == 0) && (port->pipe_loop == RTE_SCHED_PIPE_INVALID)) {
2084 port->pipe_loop = grinder->pindex;
2086 grinder_evict(port, pos);
2088 /* Look for another active pipe */
2089 if (grinder_next_pipe(port, pos)) {
2090 grinder_prefetch_pipe(port, pos);
2092 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2096 /* No active pipe found */
2097 port->busy_grinders --;
2099 grinder->state = e_GRINDER_PREFETCH_PIPE;
2104 rte_panic("Algorithmic error (invalid state)\n");
2110 rte_sched_port_time_resync(struct rte_sched_port *port)
2112 uint64_t cycles = rte_get_tsc_cycles();
2113 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2114 double bytes_diff = ((double) cycles_diff) / port->cycles_per_byte;
2116 /* Advance port time */
2117 port->time_cpu_cycles = cycles;
2118 port->time_cpu_bytes += (uint64_t) bytes_diff;
2119 if (port->time < port->time_cpu_bytes) {
2120 port->time = port->time_cpu_bytes;
2123 /* Reset pipe loop detection */
2124 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2128 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2132 /* Check if any exception flag is set */
2133 exceptions = (second_pass && port->busy_grinders == 0) ||
2134 (port->pipe_exhaustion == 1);
2136 /* Clear exception flags */
2137 port->pipe_exhaustion = 0;
2143 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2147 port->pkts_out = pkts;
2148 port->n_pkts_out = 0;
2150 rte_sched_port_time_resync(port);
2152 /* Take each queue in the grinder one step further */
2153 for (i = 0, count = 0; ; i ++) {
2154 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2155 if ((count == n_pkts) ||
2156 rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {