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 #ifdef RTE_SCHED_VECTOR
56 #include <immintrin.h>
59 #define RTE_SCHED_TB_RATE_CONFIG_ERR (1e-7)
60 #define RTE_SCHED_WRR_SHIFT 3
61 #define RTE_SCHED_GRINDER_PCACHE_SIZE (64 / RTE_SCHED_QUEUES_PER_PIPE)
62 #define RTE_SCHED_PIPE_INVALID UINT32_MAX
63 #define RTE_SCHED_BMP_POS_INVALID UINT32_MAX
65 struct rte_sched_subport {
66 /* Token bucket (TB) */
67 uint64_t tb_time; /* time of last update */
69 uint32_t tb_credits_per_period;
73 /* Traffic classes (TCs) */
74 uint64_t tc_time; /* time of next update */
75 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
76 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
79 /* TC oversubscription */
81 uint32_t tc_ov_wm_min;
82 uint32_t tc_ov_wm_max;
83 uint8_t tc_ov_period_id;
89 struct rte_sched_subport_stats stats;
92 struct rte_sched_pipe_profile {
93 /* Token bucket (TB) */
95 uint32_t tb_credits_per_period;
98 /* Pipe traffic classes */
100 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
101 uint8_t tc_ov_weight;
104 uint8_t wrr_cost[RTE_SCHED_QUEUES_PER_PIPE];
107 struct rte_sched_pipe {
108 /* Token bucket (TB) */
109 uint64_t tb_time; /* time of last update */
112 /* Pipe profile and flags */
115 /* Traffic classes (TCs) */
116 uint64_t tc_time; /* time of next update */
117 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
119 /* Weighted Round Robin (WRR) */
120 uint8_t wrr_tokens[RTE_SCHED_QUEUES_PER_PIPE];
122 /* TC oversubscription */
123 uint32_t tc_ov_credits;
124 uint8_t tc_ov_period_id;
126 } __rte_cache_aligned;
128 struct rte_sched_queue {
133 struct rte_sched_queue_extra {
134 struct rte_sched_queue_stats stats;
141 e_GRINDER_PREFETCH_PIPE = 0,
142 e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
143 e_GRINDER_PREFETCH_MBUF,
148 * Path through the scheduler hierarchy used by the scheduler enqueue
149 * operation to identify the destination queue for the current
150 * packet. Stored in the field pkt.hash.sched of struct rte_mbuf of
151 * each packet, typically written by the classification stage and read
152 * by scheduler enqueue.
154 struct rte_sched_port_hierarchy {
155 uint16_t queue:2; /**< Queue ID (0 .. 3) */
156 uint16_t traffic_class:2; /**< Traffic class ID (0 .. 3)*/
157 uint32_t color:2; /**< Color */
159 uint16_t subport; /**< Subport ID */
160 uint32_t pipe; /**< Pipe ID */
163 struct rte_sched_grinder {
165 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
166 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
171 enum grinder_state state;
174 struct rte_sched_subport *subport;
175 struct rte_sched_pipe *pipe;
176 struct rte_sched_pipe_profile *pipe_params;
179 uint8_t tccache_qmask[4];
180 uint32_t tccache_qindex[4];
186 struct rte_sched_queue *queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
187 struct rte_mbuf **qbase[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
188 uint32_t qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
192 struct rte_mbuf *pkt;
195 uint16_t wrr_tokens[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
196 uint16_t wrr_mask[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
197 uint8_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
200 struct rte_sched_port {
201 /* User parameters */
202 uint32_t n_subports_per_port;
203 uint32_t n_pipes_per_subport;
206 uint32_t frame_overhead;
207 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
208 uint32_t n_pipe_profiles;
209 uint32_t pipe_tc3_rate_max;
211 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][e_RTE_METER_COLORS];
215 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cyles */
216 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
217 uint64_t time; /* Current NIC TX time measured in bytes */
218 double cycles_per_byte; /* CPU cycles per byte */
220 /* Scheduling loop detection */
222 uint32_t pipe_exhaustion;
225 struct rte_bitmap *bmp;
226 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
229 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
230 uint32_t busy_grinders;
231 struct rte_mbuf **pkts_out;
234 /* Queue base calculation */
235 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
238 /* Large data structures */
239 struct rte_sched_subport *subport;
240 struct rte_sched_pipe *pipe;
241 struct rte_sched_queue *queue;
242 struct rte_sched_queue_extra *queue_extra;
243 struct rte_sched_pipe_profile *pipe_profiles;
245 struct rte_mbuf **queue_array;
246 uint8_t memory[0] __rte_cache_aligned;
247 } __rte_cache_aligned;
249 enum rte_sched_port_array {
250 e_RTE_SCHED_PORT_ARRAY_SUBPORT = 0,
251 e_RTE_SCHED_PORT_ARRAY_PIPE,
252 e_RTE_SCHED_PORT_ARRAY_QUEUE,
253 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA,
254 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES,
255 e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY,
256 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY,
257 e_RTE_SCHED_PORT_ARRAY_TOTAL,
260 #ifdef RTE_SCHED_COLLECT_STATS
262 static inline uint32_t
263 rte_sched_port_queues_per_subport(struct rte_sched_port *port)
265 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport;
270 static inline uint32_t
271 rte_sched_port_queues_per_port(struct rte_sched_port *port)
273 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport * port->n_subports_per_port;
277 rte_sched_port_check_params(struct rte_sched_port_params *params)
285 if ((params->socket < 0) || (params->socket >= RTE_MAX_NUMA_NODES))
289 if (params->rate == 0)
293 if (params->mtu == 0)
296 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
297 if (params->n_subports_per_port == 0 ||
298 params->n_subports_per_port > 1u << 16 ||
299 !rte_is_power_of_2(params->n_subports_per_port))
302 /* n_pipes_per_subport: non-zero, power of 2 */
303 if (params->n_pipes_per_subport == 0 ||
304 !rte_is_power_of_2(params->n_pipes_per_subport))
307 /* qsize: non-zero, power of 2,
308 * no bigger than 32K (due to 16-bit read/write pointers)
310 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
311 uint16_t qsize = params->qsize[i];
313 if (qsize == 0 || !rte_is_power_of_2(qsize))
317 /* pipe_profiles and n_pipe_profiles */
318 if (params->pipe_profiles == NULL ||
319 params->n_pipe_profiles == 0 ||
320 params->n_pipe_profiles > RTE_SCHED_PIPE_PROFILES_PER_PORT)
323 for (i = 0; i < params->n_pipe_profiles; i++) {
324 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
326 /* TB rate: non-zero, not greater than port rate */
327 if (p->tb_rate == 0 || p->tb_rate > params->rate)
330 /* TB size: non-zero */
334 /* TC rate: non-zero, less than pipe rate */
335 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
336 if (p->tc_rate[j] == 0 || p->tc_rate[j] > p->tb_rate)
340 /* TC period: non-zero */
341 if (p->tc_period == 0)
344 #ifdef RTE_SCHED_SUBPORT_TC_OV
345 /* TC3 oversubscription weight: non-zero */
346 if (p->tc_ov_weight == 0)
350 /* Queue WRR weights: non-zero */
351 for (j = 0; j < RTE_SCHED_QUEUES_PER_PIPE; j++) {
352 if (p->wrr_weights[j] == 0)
361 rte_sched_port_get_array_base(struct rte_sched_port_params *params, enum rte_sched_port_array array)
363 uint32_t n_subports_per_port = params->n_subports_per_port;
364 uint32_t n_pipes_per_subport = params->n_pipes_per_subport;
365 uint32_t n_pipes_per_port = n_pipes_per_subport * n_subports_per_port;
366 uint32_t n_queues_per_port = RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport * n_subports_per_port;
368 uint32_t size_subport = n_subports_per_port * sizeof(struct rte_sched_subport);
369 uint32_t size_pipe = n_pipes_per_port * sizeof(struct rte_sched_pipe);
370 uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue);
371 uint32_t size_queue_extra
372 = n_queues_per_port * sizeof(struct rte_sched_queue_extra);
373 uint32_t size_pipe_profiles
374 = RTE_SCHED_PIPE_PROFILES_PER_PORT * sizeof(struct rte_sched_pipe_profile);
375 uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port);
376 uint32_t size_per_pipe_queue_array, size_queue_array;
380 size_per_pipe_queue_array = 0;
381 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
382 size_per_pipe_queue_array += RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS
383 * params->qsize[i] * sizeof(struct rte_mbuf *);
385 size_queue_array = n_pipes_per_port * size_per_pipe_queue_array;
389 if (array == e_RTE_SCHED_PORT_ARRAY_SUBPORT)
391 base += RTE_CACHE_LINE_ROUNDUP(size_subport);
393 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE)
395 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
397 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE)
399 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
401 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA)
403 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
405 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES)
407 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
409 if (array == e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY)
411 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
413 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY)
415 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
421 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
423 uint32_t size0, size1;
426 status = rte_sched_port_check_params(params);
428 RTE_LOG(NOTICE, SCHED,
429 "Port scheduler params check failed (%d)\n", status);
434 size0 = sizeof(struct rte_sched_port);
435 size1 = rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_TOTAL);
437 return (size0 + size1);
441 rte_sched_port_config_qsize(struct rte_sched_port *port)
444 port->qsize_add[0] = 0;
445 port->qsize_add[1] = port->qsize_add[0] + port->qsize[0];
446 port->qsize_add[2] = port->qsize_add[1] + port->qsize[0];
447 port->qsize_add[3] = port->qsize_add[2] + port->qsize[0];
450 port->qsize_add[4] = port->qsize_add[3] + port->qsize[0];
451 port->qsize_add[5] = port->qsize_add[4] + port->qsize[1];
452 port->qsize_add[6] = port->qsize_add[5] + port->qsize[1];
453 port->qsize_add[7] = port->qsize_add[6] + port->qsize[1];
456 port->qsize_add[8] = port->qsize_add[7] + port->qsize[1];
457 port->qsize_add[9] = port->qsize_add[8] + port->qsize[2];
458 port->qsize_add[10] = port->qsize_add[9] + port->qsize[2];
459 port->qsize_add[11] = port->qsize_add[10] + port->qsize[2];
462 port->qsize_add[12] = port->qsize_add[11] + port->qsize[2];
463 port->qsize_add[13] = port->qsize_add[12] + port->qsize[3];
464 port->qsize_add[14] = port->qsize_add[13] + port->qsize[3];
465 port->qsize_add[15] = port->qsize_add[14] + port->qsize[3];
467 port->qsize_sum = port->qsize_add[15] + port->qsize[3];
471 rte_sched_port_log_pipe_profile(struct rte_sched_port *port, uint32_t i)
473 struct rte_sched_pipe_profile *p = port->pipe_profiles + i;
475 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
476 " Token bucket: period = %u, credits per period = %u, size = %u\n"
477 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
478 " Traffic class 3 oversubscription: weight = %hhu\n"
479 " WRR cost: [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu]\n",
484 p->tb_credits_per_period,
487 /* Traffic classes */
489 p->tc_credits_per_period[0],
490 p->tc_credits_per_period[1],
491 p->tc_credits_per_period[2],
492 p->tc_credits_per_period[3],
494 /* Traffic class 3 oversubscription */
498 p->wrr_cost[ 0], p->wrr_cost[ 1], p->wrr_cost[ 2], p->wrr_cost[ 3],
499 p->wrr_cost[ 4], p->wrr_cost[ 5], p->wrr_cost[ 6], p->wrr_cost[ 7],
500 p->wrr_cost[ 8], p->wrr_cost[ 9], p->wrr_cost[10], p->wrr_cost[11],
501 p->wrr_cost[12], p->wrr_cost[13], p->wrr_cost[14], p->wrr_cost[15]);
504 static inline uint64_t
505 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
507 uint64_t time = time_ms;
509 time = (time * rate) / 1000;
515 rte_sched_port_config_pipe_profile_table(struct rte_sched_port *port, struct rte_sched_port_params *params)
519 for (i = 0; i < port->n_pipe_profiles; i++) {
520 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
521 struct rte_sched_pipe_profile *dst = port->pipe_profiles + i;
524 if (src->tb_rate == params->rate) {
525 dst->tb_credits_per_period = 1;
528 double tb_rate = (double) src->tb_rate
529 / (double) params->rate;
530 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
532 rte_approx(tb_rate, d,
533 &dst->tb_credits_per_period, &dst->tb_period);
535 dst->tb_size = src->tb_size;
537 /* Traffic Classes */
538 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
541 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++)
542 dst->tc_credits_per_period[j]
543 = rte_sched_time_ms_to_bytes(src->tc_period,
546 #ifdef RTE_SCHED_SUBPORT_TC_OV
547 dst->tc_ov_weight = src->tc_ov_weight;
551 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
552 uint32_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
553 uint32_t lcd, lcd1, lcd2;
556 qindex = j * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS;
558 wrr_cost[0] = src->wrr_weights[qindex];
559 wrr_cost[1] = src->wrr_weights[qindex + 1];
560 wrr_cost[2] = src->wrr_weights[qindex + 2];
561 wrr_cost[3] = src->wrr_weights[qindex + 3];
563 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
564 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
565 lcd = rte_get_lcd(lcd1, lcd2);
567 wrr_cost[0] = lcd / wrr_cost[0];
568 wrr_cost[1] = lcd / wrr_cost[1];
569 wrr_cost[2] = lcd / wrr_cost[2];
570 wrr_cost[3] = lcd / wrr_cost[3];
572 dst->wrr_cost[qindex] = (uint8_t) wrr_cost[0];
573 dst->wrr_cost[qindex + 1] = (uint8_t) wrr_cost[1];
574 dst->wrr_cost[qindex + 2] = (uint8_t) wrr_cost[2];
575 dst->wrr_cost[qindex + 3] = (uint8_t) wrr_cost[3];
578 rte_sched_port_log_pipe_profile(port, i);
581 port->pipe_tc3_rate_max = 0;
582 for (i = 0; i < port->n_pipe_profiles; i++) {
583 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
584 uint32_t pipe_tc3_rate = src->tc_rate[3];
586 if (port->pipe_tc3_rate_max < pipe_tc3_rate)
587 port->pipe_tc3_rate_max = pipe_tc3_rate;
591 struct rte_sched_port *
592 rte_sched_port_config(struct rte_sched_port_params *params)
594 struct rte_sched_port *port = NULL;
595 uint32_t mem_size, bmp_mem_size, n_queues_per_port, i;
597 /* Check user parameters. Determine the amount of memory to allocate */
598 mem_size = rte_sched_port_get_memory_footprint(params);
602 /* Allocate memory to store the data structures */
603 port = rte_zmalloc("qos_params", mem_size, RTE_CACHE_LINE_SIZE);
607 /* compile time checks */
608 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
609 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS & (RTE_SCHED_PORT_N_GRINDERS - 1));
611 /* User parameters */
612 port->n_subports_per_port = params->n_subports_per_port;
613 port->n_pipes_per_subport = params->n_pipes_per_subport;
614 port->rate = params->rate;
615 port->mtu = params->mtu + params->frame_overhead;
616 port->frame_overhead = params->frame_overhead;
617 memcpy(port->qsize, params->qsize, sizeof(params->qsize));
618 port->n_pipe_profiles = params->n_pipe_profiles;
621 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
624 for (j = 0; j < e_RTE_METER_COLORS; j++) {
625 /* if min/max are both zero, then RED is disabled */
626 if ((params->red_params[i][j].min_th |
627 params->red_params[i][j].max_th) == 0) {
631 if (rte_red_config_init(&port->red_config[i][j],
632 params->red_params[i][j].wq_log2,
633 params->red_params[i][j].min_th,
634 params->red_params[i][j].max_th,
635 params->red_params[i][j].maxp_inv) != 0) {
643 port->time_cpu_cycles = rte_get_tsc_cycles();
644 port->time_cpu_bytes = 0;
646 port->cycles_per_byte = ((double) rte_get_tsc_hz()) / ((double) params->rate);
648 /* Scheduling loop detection */
649 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
650 port->pipe_exhaustion = 0;
653 port->busy_grinders = 0;
654 port->pkts_out = NULL;
655 port->n_pkts_out = 0;
657 /* Queue base calculation */
658 rte_sched_port_config_qsize(port);
660 /* Large data structures */
661 port->subport = (struct rte_sched_subport *)
662 (port->memory + rte_sched_port_get_array_base(params,
663 e_RTE_SCHED_PORT_ARRAY_SUBPORT));
664 port->pipe = (struct rte_sched_pipe *)
665 (port->memory + rte_sched_port_get_array_base(params,
666 e_RTE_SCHED_PORT_ARRAY_PIPE));
667 port->queue = (struct rte_sched_queue *)
668 (port->memory + rte_sched_port_get_array_base(params,
669 e_RTE_SCHED_PORT_ARRAY_QUEUE));
670 port->queue_extra = (struct rte_sched_queue_extra *)
671 (port->memory + rte_sched_port_get_array_base(params,
672 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA));
673 port->pipe_profiles = (struct rte_sched_pipe_profile *)
674 (port->memory + rte_sched_port_get_array_base(params,
675 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES));
676 port->bmp_array = port->memory
677 + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY);
678 port->queue_array = (struct rte_mbuf **)
679 (port->memory + rte_sched_port_get_array_base(params,
680 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY));
682 /* Pipe profile table */
683 rte_sched_port_config_pipe_profile_table(port, params);
686 n_queues_per_port = rte_sched_port_queues_per_port(port);
687 bmp_mem_size = rte_bitmap_get_memory_footprint(n_queues_per_port);
688 port->bmp = rte_bitmap_init(n_queues_per_port, port->bmp_array,
690 if (port->bmp == NULL) {
691 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 */
709 rte_bitmap_free(port->bmp);
714 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
716 struct rte_sched_subport *s = port->subport + i;
718 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
719 " Token bucket: period = %u, credits per period = %u, size = %u\n"
720 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
721 " Traffic class 3 oversubscription: wm min = %u, wm max = %u\n",
726 s->tb_credits_per_period,
729 /* Traffic classes */
731 s->tc_credits_per_period[0],
732 s->tc_credits_per_period[1],
733 s->tc_credits_per_period[2],
734 s->tc_credits_per_period[3],
736 /* Traffic class 3 oversubscription */
742 rte_sched_subport_config(struct rte_sched_port *port,
744 struct rte_sched_subport_params *params)
746 struct rte_sched_subport *s;
749 /* Check user parameters */
751 subport_id >= port->n_subports_per_port ||
755 if (params->tb_rate == 0 || params->tb_rate > port->rate)
758 if (params->tb_size == 0)
761 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
762 if (params->tc_rate[i] == 0 ||
763 params->tc_rate[i] > params->tb_rate)
767 if (params->tc_period == 0)
770 s = port->subport + subport_id;
772 /* Token Bucket (TB) */
773 if (params->tb_rate == port->rate) {
774 s->tb_credits_per_period = 1;
777 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
778 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
780 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
783 s->tb_size = params->tb_size;
784 s->tb_time = port->time;
785 s->tb_credits = s->tb_size / 2;
787 /* Traffic Classes (TCs) */
788 s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
789 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
790 s->tc_credits_per_period[i]
791 = rte_sched_time_ms_to_bytes(params->tc_period,
794 s->tc_time = port->time + s->tc_period;
795 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
796 s->tc_credits[i] = s->tc_credits_per_period[i];
798 #ifdef RTE_SCHED_SUBPORT_TC_OV
799 /* TC oversubscription */
800 s->tc_ov_wm_min = port->mtu;
801 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
802 port->pipe_tc3_rate_max);
803 s->tc_ov_wm = s->tc_ov_wm_max;
804 s->tc_ov_period_id = 0;
810 rte_sched_port_log_subport_config(port, subport_id);
816 rte_sched_pipe_config(struct rte_sched_port *port,
819 int32_t pipe_profile)
821 struct rte_sched_subport *s;
822 struct rte_sched_pipe *p;
823 struct rte_sched_pipe_profile *params;
824 uint32_t deactivate, profile, i;
826 /* Check user parameters */
827 profile = (uint32_t) pipe_profile;
828 deactivate = (pipe_profile < 0);
831 subport_id >= port->n_subports_per_port ||
832 pipe_id >= port->n_pipes_per_subport ||
833 (!deactivate && profile >= port->n_pipe_profiles))
837 /* Check that subport configuration is valid */
838 s = port->subport + subport_id;
839 if (s->tb_period == 0)
842 p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
844 /* Handle the case when pipe already has a valid configuration */
846 params = port->pipe_profiles + p->profile;
848 #ifdef RTE_SCHED_SUBPORT_TC_OV
849 double subport_tc3_rate = (double) s->tc_credits_per_period[3]
850 / (double) s->tc_period;
851 double pipe_tc3_rate = (double) params->tc_credits_per_period[3]
852 / (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));
874 /* Apply the new pipe configuration */
875 p->profile = profile;
876 params = port->pipe_profiles + p->profile;
878 /* Token Bucket (TB) */
879 p->tb_time = port->time;
880 p->tb_credits = params->tb_size / 2;
882 /* Traffic Classes (TCs) */
883 p->tc_time = port->time + params->tc_period;
884 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
885 p->tc_credits[i] = params->tc_credits_per_period[i];
887 #ifdef RTE_SCHED_SUBPORT_TC_OV
889 /* Subport TC3 oversubscription */
890 double subport_tc3_rate = (double) s->tc_credits_per_period[3]
891 / (double) s->tc_period;
892 double pipe_tc3_rate = (double) params->tc_credits_per_period[3]
893 / (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_port_pkt_write(struct rte_mbuf *pkt,
915 uint32_t subport, uint32_t pipe, uint32_t traffic_class,
916 uint32_t queue, enum rte_meter_color color)
918 struct rte_sched_port_hierarchy *sched
919 = (struct rte_sched_port_hierarchy *) &pkt->hash.sched;
921 RTE_BUILD_BUG_ON(sizeof(*sched) > sizeof(pkt->hash.sched));
923 sched->color = (uint32_t) color;
924 sched->subport = subport;
926 sched->traffic_class = traffic_class;
927 sched->queue = queue;
931 rte_sched_port_pkt_read_tree_path(const struct rte_mbuf *pkt,
932 uint32_t *subport, uint32_t *pipe,
933 uint32_t *traffic_class, uint32_t *queue)
935 const struct rte_sched_port_hierarchy *sched
936 = (const struct rte_sched_port_hierarchy *) &pkt->hash.sched;
938 *subport = sched->subport;
940 *traffic_class = sched->traffic_class;
941 *queue = sched->queue;
945 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
947 const struct rte_sched_port_hierarchy *sched
948 = (const struct rte_sched_port_hierarchy *) &pkt->hash.sched;
950 return (enum rte_meter_color) sched->color;
954 rte_sched_subport_read_stats(struct rte_sched_port *port,
956 struct rte_sched_subport_stats *stats,
959 struct rte_sched_subport *s;
961 /* Check user parameters */
962 if (port == NULL || subport_id >= port->n_subports_per_port ||
963 stats == NULL || tc_ov == NULL)
966 s = port->subport + subport_id;
968 /* Copy subport stats and clear */
969 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
970 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
972 /* Subport TC ovesubscription status */
979 rte_sched_queue_read_stats(struct rte_sched_port *port,
981 struct rte_sched_queue_stats *stats,
984 struct rte_sched_queue *q;
985 struct rte_sched_queue_extra *qe;
987 /* Check user parameters */
988 if ((port == NULL) ||
989 (queue_id >= rte_sched_port_queues_per_port(port)) ||
994 q = port->queue + queue_id;
995 qe = port->queue_extra + queue_id;
997 /* Copy queue stats and clear */
998 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
999 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1002 *qlen = q->qw - q->qr;
1007 static inline uint32_t
1008 rte_sched_port_qindex(struct rte_sched_port *port, uint32_t subport, uint32_t pipe, uint32_t traffic_class, uint32_t queue)
1012 result = subport * port->n_pipes_per_subport + pipe;
1013 result = result * RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE + traffic_class;
1014 result = result * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
1019 static inline struct rte_mbuf **
1020 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
1022 uint32_t pindex = qindex >> 4;
1023 uint32_t qpos = qindex & 0xF;
1025 return (port->queue_array + pindex * port->qsize_sum + port->qsize_add[qpos]);
1028 static inline uint16_t
1029 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
1031 uint32_t tc = (qindex >> 2) & 0x3;
1033 return port->qsize[tc];
1036 #ifdef RTE_SCHED_DEBUG
1039 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1041 struct rte_sched_queue *queue = port->queue + qindex;
1043 return (queue->qr == queue->qw);
1047 rte_sched_port_queue_is_full(struct rte_sched_port *port, uint32_t qindex)
1049 struct rte_sched_queue *queue = port->queue + qindex;
1050 uint16_t qsize = rte_sched_port_qsize(port, qindex);
1051 uint16_t qlen = queue->qw - queue->qr;
1053 return (qlen >= qsize);
1056 #endif /* RTE_SCHED_DEBUG */
1058 #ifdef RTE_SCHED_COLLECT_STATS
1061 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1063 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1064 uint32_t tc_index = (qindex >> 2) & 0x3;
1065 uint32_t pkt_len = pkt->pkt_len;
1067 s->stats.n_pkts_tc[tc_index] += 1;
1068 s->stats.n_bytes_tc[tc_index] += pkt_len;
1072 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1074 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1075 uint32_t tc_index = (qindex >> 2) & 0x3;
1076 uint32_t pkt_len = pkt->pkt_len;
1078 s->stats.n_pkts_tc_dropped[tc_index] += 1;
1079 s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1083 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1085 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1086 uint32_t pkt_len = pkt->pkt_len;
1088 qe->stats.n_pkts += 1;
1089 qe->stats.n_bytes += pkt_len;
1093 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1095 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1096 uint32_t pkt_len = pkt->pkt_len;
1098 qe->stats.n_pkts_dropped += 1;
1099 qe->stats.n_bytes_dropped += pkt_len;
1102 #endif /* RTE_SCHED_COLLECT_STATS */
1104 #ifdef RTE_SCHED_RED
1107 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1109 struct rte_sched_queue_extra *qe;
1110 struct rte_red_config *red_cfg;
1111 struct rte_red *red;
1113 enum rte_meter_color color;
1115 tc_index = (qindex >> 2) & 0x3;
1116 color = rte_sched_port_pkt_read_color(pkt);
1117 red_cfg = &port->red_config[tc_index][color];
1119 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1122 qe = port->queue_extra + qindex;
1125 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1129 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1131 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1132 struct rte_red *red = &qe->red;
1134 rte_red_mark_queue_empty(red, port->time);
1139 #define rte_sched_port_red_drop(port, pkt, qindex, qlen) 0
1141 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1143 #endif /* RTE_SCHED_RED */
1145 #ifdef RTE_SCHED_DEBUG
1148 debug_pipe_is_empty(struct rte_sched_port *port, uint32_t pindex)
1152 qindex = pindex << 4;
1154 for (i = 0; i < 16; i++) {
1155 uint32_t queue_empty = rte_sched_port_queue_is_empty(port, qindex + i);
1156 uint32_t bmp_bit_clear = (rte_bitmap_get(port->bmp, qindex + i) == 0);
1158 if (queue_empty != bmp_bit_clear)
1159 rte_panic("Queue status mismatch for queue %u of pipe %u\n", i, pindex);
1169 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1176 rte_panic("Empty slab at position %u\n", bmp_pos);
1179 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1180 if (mask & bmp_slab) {
1181 if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1182 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1189 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1193 #endif /* RTE_SCHED_DEBUG */
1195 static inline uint32_t
1196 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port,
1197 struct rte_mbuf *pkt)
1199 struct rte_sched_queue *q;
1200 #ifdef RTE_SCHED_COLLECT_STATS
1201 struct rte_sched_queue_extra *qe;
1203 uint32_t subport, pipe, traffic_class, queue, qindex;
1205 rte_sched_port_pkt_read_tree_path(pkt, &subport, &pipe, &traffic_class, &queue);
1207 qindex = rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1208 q = port->queue + qindex;
1210 #ifdef RTE_SCHED_COLLECT_STATS
1211 qe = port->queue_extra + qindex;
1219 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1220 uint32_t qindex, struct rte_mbuf **qbase)
1222 struct rte_sched_queue *q;
1223 struct rte_mbuf **q_qw;
1226 q = port->queue + qindex;
1227 qsize = rte_sched_port_qsize(port, qindex);
1228 q_qw = qbase + (q->qw & (qsize - 1));
1230 rte_prefetch0(q_qw);
1231 rte_bitmap_prefetch0(port->bmp, qindex);
1235 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex,
1236 struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1238 struct rte_sched_queue *q;
1242 q = port->queue + qindex;
1243 qsize = rte_sched_port_qsize(port, qindex);
1244 qlen = q->qw - q->qr;
1246 /* Drop the packet (and update drop stats) when queue is full */
1247 if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) ||
1249 rte_pktmbuf_free(pkt);
1250 #ifdef RTE_SCHED_COLLECT_STATS
1251 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt);
1252 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt);
1257 /* Enqueue packet */
1258 qbase[q->qw & (qsize - 1)] = pkt;
1261 /* Activate queue in the port bitmap */
1262 rte_bitmap_set(port->bmp, qindex);
1265 #ifdef RTE_SCHED_COLLECT_STATS
1266 rte_sched_port_update_subport_stats(port, qindex, pkt);
1267 rte_sched_port_update_queue_stats(port, qindex, pkt);
1275 * The enqueue function implements a 4-level pipeline with each stage
1276 * processing two different packets. The purpose of using a pipeline
1277 * is to hide the latency of prefetching the data structures. The
1278 * naming convention is presented in the diagram below:
1280 * p00 _______ p10 _______ p20 _______ p30 _______
1281 * ----->| |----->| |----->| |----->| |----->
1282 * | 0 | | 1 | | 2 | | 3 |
1283 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1288 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1291 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1292 *pkt30, *pkt31, *pkt_last;
1293 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1294 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1295 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1296 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1302 * Less then 6 input packets available, which is not enough to
1305 if (unlikely(n_pkts < 6)) {
1306 struct rte_mbuf **q_base[5];
1309 /* Prefetch the mbuf structure of each packet */
1310 for (i = 0; i < n_pkts; i++)
1311 rte_prefetch0(pkts[i]);
1313 /* Prefetch the queue structure for each queue */
1314 for (i = 0; i < n_pkts; i++)
1315 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port,
1318 /* Prefetch the write pointer location of each queue */
1319 for (i = 0; i < n_pkts; i++) {
1320 q_base[i] = rte_sched_port_qbase(port, q[i]);
1321 rte_sched_port_enqueue_qwa_prefetch0(port, q[i],
1325 /* Write each packet to its queue */
1326 for (i = 0; i < n_pkts; i++)
1327 result += rte_sched_port_enqueue_qwa(port, q[i],
1328 q_base[i], pkts[i]);
1333 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1336 rte_prefetch0(pkt20);
1337 rte_prefetch0(pkt21);
1341 rte_prefetch0(pkt10);
1342 rte_prefetch0(pkt11);
1344 q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1345 q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1349 rte_prefetch0(pkt00);
1350 rte_prefetch0(pkt01);
1352 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1353 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1355 q20_base = rte_sched_port_qbase(port, q20);
1356 q21_base = rte_sched_port_qbase(port, q21);
1357 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1358 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1360 /* Run the pipeline */
1361 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1362 /* Propagate stage inputs */
1373 q30_base = q20_base;
1374 q31_base = q21_base;
1376 /* Stage 0: Get packets in */
1378 pkt01 = pkts[i + 1];
1379 rte_prefetch0(pkt00);
1380 rte_prefetch0(pkt01);
1382 /* Stage 1: Prefetch queue structure storing queue pointers */
1383 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1384 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1386 /* Stage 2: Prefetch queue write location */
1387 q20_base = rte_sched_port_qbase(port, q20);
1388 q21_base = rte_sched_port_qbase(port, q21);
1389 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1390 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1392 /* Stage 3: Write packet to queue and activate queue */
1393 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1394 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1395 result += r30 + r31;
1399 * Drain the pipeline (exactly 6 packets).
1400 * Handle the last packet in the case
1401 * of an odd number of input packets.
1403 pkt_last = pkts[n_pkts - 1];
1404 rte_prefetch0(pkt_last);
1406 q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1407 q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1409 q10_base = rte_sched_port_qbase(port, q10);
1410 q11_base = rte_sched_port_qbase(port, q11);
1411 rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1412 rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1414 r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1415 r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1416 result += r20 + r21;
1418 q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1420 q00_base = rte_sched_port_qbase(port, q00);
1421 q01_base = rte_sched_port_qbase(port, q01);
1422 rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1423 rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1425 r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1426 r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1427 result += r10 + r11;
1429 q_last_base = rte_sched_port_qbase(port, q_last);
1430 rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1432 r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1433 r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1434 result += r00 + r01;
1437 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1444 #ifndef RTE_SCHED_SUBPORT_TC_OV
1447 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1449 struct rte_sched_grinder *grinder = port->grinder + pos;
1450 struct rte_sched_subport *subport = grinder->subport;
1451 struct rte_sched_pipe *pipe = grinder->pipe;
1452 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1456 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1457 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1458 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1459 subport->tb_time += n_periods * subport->tb_period;
1462 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1463 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1464 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1465 pipe->tb_time += n_periods * params->tb_period;
1468 if (unlikely(port->time >= subport->tc_time)) {
1469 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1470 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1471 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1472 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1473 subport->tc_time = port->time + subport->tc_period;
1477 if (unlikely(port->time >= pipe->tc_time)) {
1478 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1479 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1480 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1481 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1482 pipe->tc_time = port->time + params->tc_period;
1488 static inline uint32_t
1489 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1491 struct rte_sched_grinder *grinder = port->grinder + pos;
1492 struct rte_sched_subport *subport = grinder->subport;
1493 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1494 uint32_t tc_ov_consumption_max;
1495 uint32_t tc_ov_wm = subport->tc_ov_wm;
1497 if (subport->tc_ov == 0)
1498 return subport->tc_ov_wm_max;
1500 tc_ov_consumption[0] = subport->tc_credits_per_period[0] - subport->tc_credits[0];
1501 tc_ov_consumption[1] = subport->tc_credits_per_period[1] - subport->tc_credits[1];
1502 tc_ov_consumption[2] = subport->tc_credits_per_period[2] - subport->tc_credits[2];
1503 tc_ov_consumption[3] = subport->tc_credits_per_period[3] - subport->tc_credits[3];
1505 tc_ov_consumption_max = subport->tc_credits_per_period[3] -
1506 (tc_ov_consumption[0] + tc_ov_consumption[1] + tc_ov_consumption[2]);
1508 if (tc_ov_consumption[3] > (tc_ov_consumption_max - port->mtu)) {
1509 tc_ov_wm -= tc_ov_wm >> 7;
1510 if (tc_ov_wm < subport->tc_ov_wm_min)
1511 tc_ov_wm = subport->tc_ov_wm_min;
1516 tc_ov_wm += (tc_ov_wm >> 7) + 1;
1517 if (tc_ov_wm > subport->tc_ov_wm_max)
1518 tc_ov_wm = subport->tc_ov_wm_max;
1524 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1526 struct rte_sched_grinder *grinder = port->grinder + pos;
1527 struct rte_sched_subport *subport = grinder->subport;
1528 struct rte_sched_pipe *pipe = grinder->pipe;
1529 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1533 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1534 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1535 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1536 subport->tb_time += n_periods * subport->tb_period;
1539 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1540 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1541 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1542 pipe->tb_time += n_periods * params->tb_period;
1545 if (unlikely(port->time >= subport->tc_time)) {
1546 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
1548 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1549 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1550 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1551 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1553 subport->tc_time = port->time + subport->tc_period;
1554 subport->tc_ov_period_id++;
1558 if (unlikely(port->time >= pipe->tc_time)) {
1559 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1560 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1561 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1562 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1563 pipe->tc_time = port->time + params->tc_period;
1566 /* Pipe TCs - Oversubscription */
1567 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
1568 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
1570 pipe->tc_ov_period_id = subport->tc_ov_period_id;
1574 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
1577 #ifndef RTE_SCHED_SUBPORT_TC_OV
1580 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1582 struct rte_sched_grinder *grinder = port->grinder + pos;
1583 struct rte_sched_subport *subport = grinder->subport;
1584 struct rte_sched_pipe *pipe = grinder->pipe;
1585 struct rte_mbuf *pkt = grinder->pkt;
1586 uint32_t tc_index = grinder->tc_index;
1587 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1588 uint32_t subport_tb_credits = subport->tb_credits;
1589 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1590 uint32_t pipe_tb_credits = pipe->tb_credits;
1591 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1594 /* Check queue credits */
1595 enough_credits = (pkt_len <= subport_tb_credits) &&
1596 (pkt_len <= subport_tc_credits) &&
1597 (pkt_len <= pipe_tb_credits) &&
1598 (pkt_len <= pipe_tc_credits);
1600 if (!enough_credits)
1603 /* Update port credits */
1604 subport->tb_credits -= pkt_len;
1605 subport->tc_credits[tc_index] -= pkt_len;
1606 pipe->tb_credits -= pkt_len;
1607 pipe->tc_credits[tc_index] -= pkt_len;
1615 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1617 struct rte_sched_grinder *grinder = port->grinder + pos;
1618 struct rte_sched_subport *subport = grinder->subport;
1619 struct rte_sched_pipe *pipe = grinder->pipe;
1620 struct rte_mbuf *pkt = grinder->pkt;
1621 uint32_t tc_index = grinder->tc_index;
1622 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1623 uint32_t subport_tb_credits = subport->tb_credits;
1624 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1625 uint32_t pipe_tb_credits = pipe->tb_credits;
1626 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1627 uint32_t pipe_tc_ov_mask1[] = {UINT32_MAX, UINT32_MAX, UINT32_MAX, pipe->tc_ov_credits};
1628 uint32_t pipe_tc_ov_mask2[] = {0, 0, 0, UINT32_MAX};
1629 uint32_t pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
1632 /* Check pipe and subport credits */
1633 enough_credits = (pkt_len <= subport_tb_credits) &&
1634 (pkt_len <= subport_tc_credits) &&
1635 (pkt_len <= pipe_tb_credits) &&
1636 (pkt_len <= pipe_tc_credits) &&
1637 (pkt_len <= pipe_tc_ov_credits);
1639 if (!enough_credits)
1642 /* Update pipe and subport credits */
1643 subport->tb_credits -= pkt_len;
1644 subport->tc_credits[tc_index] -= pkt_len;
1645 pipe->tb_credits -= pkt_len;
1646 pipe->tc_credits[tc_index] -= pkt_len;
1647 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
1652 #endif /* RTE_SCHED_SUBPORT_TC_OV */
1656 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
1658 struct rte_sched_grinder *grinder = port->grinder + pos;
1659 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
1660 struct rte_mbuf *pkt = grinder->pkt;
1661 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1663 if (!grinder_credits_check(port, pos))
1666 /* Advance port time */
1667 port->time += pkt_len;
1670 port->pkts_out[port->n_pkts_out++] = pkt;
1672 grinder->wrr_tokens[grinder->qpos] += pkt_len * grinder->wrr_cost[grinder->qpos];
1673 if (queue->qr == queue->qw) {
1674 uint32_t qindex = grinder->qindex[grinder->qpos];
1676 rte_bitmap_clear(port->bmp, qindex);
1677 grinder->qmask &= ~(1 << grinder->qpos);
1678 grinder->wrr_mask[grinder->qpos] = 0;
1679 rte_sched_port_set_queue_empty_timestamp(port, qindex);
1682 /* Reset pipe loop detection */
1683 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1684 grinder->productive = 1;
1689 #ifdef RTE_SCHED_VECTOR
1692 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1694 __m128i index = _mm_set1_epi32(base_pipe);
1695 __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
1696 __m128i res = _mm_cmpeq_epi32(pipes, index);
1698 pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
1699 pipes = _mm_cmpeq_epi32(pipes, index);
1700 res = _mm_or_si128(res, pipes);
1702 if (_mm_testz_si128(res, res))
1711 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1715 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
1716 if (port->grinder_base_bmp_pos[i] == base_pipe)
1723 #endif /* RTE_SCHED_OPTIMIZATIONS */
1726 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
1728 struct rte_sched_grinder *grinder = port->grinder + pos;
1731 grinder->pcache_w = 0;
1732 grinder->pcache_r = 0;
1734 w[0] = (uint16_t) bmp_slab;
1735 w[1] = (uint16_t) (bmp_slab >> 16);
1736 w[2] = (uint16_t) (bmp_slab >> 32);
1737 w[3] = (uint16_t) (bmp_slab >> 48);
1739 grinder->pcache_qmask[grinder->pcache_w] = w[0];
1740 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
1741 grinder->pcache_w += (w[0] != 0);
1743 grinder->pcache_qmask[grinder->pcache_w] = w[1];
1744 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
1745 grinder->pcache_w += (w[1] != 0);
1747 grinder->pcache_qmask[grinder->pcache_w] = w[2];
1748 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
1749 grinder->pcache_w += (w[2] != 0);
1751 grinder->pcache_qmask[grinder->pcache_w] = w[3];
1752 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
1753 grinder->pcache_w += (w[3] != 0);
1757 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
1759 struct rte_sched_grinder *grinder = port->grinder + pos;
1762 grinder->tccache_w = 0;
1763 grinder->tccache_r = 0;
1765 b[0] = (uint8_t) (qmask & 0xF);
1766 b[1] = (uint8_t) ((qmask >> 4) & 0xF);
1767 b[2] = (uint8_t) ((qmask >> 8) & 0xF);
1768 b[3] = (uint8_t) ((qmask >> 12) & 0xF);
1770 grinder->tccache_qmask[grinder->tccache_w] = b[0];
1771 grinder->tccache_qindex[grinder->tccache_w] = qindex;
1772 grinder->tccache_w += (b[0] != 0);
1774 grinder->tccache_qmask[grinder->tccache_w] = b[1];
1775 grinder->tccache_qindex[grinder->tccache_w] = qindex + 4;
1776 grinder->tccache_w += (b[1] != 0);
1778 grinder->tccache_qmask[grinder->tccache_w] = b[2];
1779 grinder->tccache_qindex[grinder->tccache_w] = qindex + 8;
1780 grinder->tccache_w += (b[2] != 0);
1782 grinder->tccache_qmask[grinder->tccache_w] = b[3];
1783 grinder->tccache_qindex[grinder->tccache_w] = qindex + 12;
1784 grinder->tccache_w += (b[3] != 0);
1788 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
1790 struct rte_sched_grinder *grinder = port->grinder + pos;
1791 struct rte_mbuf **qbase;
1795 if (grinder->tccache_r == grinder->tccache_w)
1798 qindex = grinder->tccache_qindex[grinder->tccache_r];
1799 qbase = rte_sched_port_qbase(port, qindex);
1800 qsize = rte_sched_port_qsize(port, qindex);
1802 grinder->tc_index = (qindex >> 2) & 0x3;
1803 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
1804 grinder->qsize = qsize;
1806 grinder->qindex[0] = qindex;
1807 grinder->qindex[1] = qindex + 1;
1808 grinder->qindex[2] = qindex + 2;
1809 grinder->qindex[3] = qindex + 3;
1811 grinder->queue[0] = port->queue + qindex;
1812 grinder->queue[1] = port->queue + qindex + 1;
1813 grinder->queue[2] = port->queue + qindex + 2;
1814 grinder->queue[3] = port->queue + qindex + 3;
1816 grinder->qbase[0] = qbase;
1817 grinder->qbase[1] = qbase + qsize;
1818 grinder->qbase[2] = qbase + 2 * qsize;
1819 grinder->qbase[3] = qbase + 3 * qsize;
1821 grinder->tccache_r++;
1826 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
1828 struct rte_sched_grinder *grinder = port->grinder + pos;
1829 uint32_t pipe_qindex;
1830 uint16_t pipe_qmask;
1832 if (grinder->pcache_r < grinder->pcache_w) {
1833 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
1834 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
1835 grinder->pcache_r++;
1837 uint64_t bmp_slab = 0;
1838 uint32_t bmp_pos = 0;
1840 /* Get another non-empty pipe group */
1841 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
1844 #ifdef RTE_SCHED_DEBUG
1845 debug_check_queue_slab(port, bmp_pos, bmp_slab);
1848 /* Return if pipe group already in one of the other grinders */
1849 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
1850 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
1853 port->grinder_base_bmp_pos[pos] = bmp_pos;
1855 /* Install new pipe group into grinder's pipe cache */
1856 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
1858 pipe_qmask = grinder->pcache_qmask[0];
1859 pipe_qindex = grinder->pcache_qindex[0];
1860 grinder->pcache_r = 1;
1863 /* Install new pipe in the grinder */
1864 grinder->pindex = pipe_qindex >> 4;
1865 grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
1866 grinder->pipe = port->pipe + grinder->pindex;
1867 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
1868 grinder->productive = 0;
1870 grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
1871 grinder_next_tc(port, pos);
1873 /* Check for pipe exhaustion */
1874 if (grinder->pindex == port->pipe_loop) {
1875 port->pipe_exhaustion = 1;
1876 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1884 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
1886 struct rte_sched_grinder *grinder = port->grinder + pos;
1887 struct rte_sched_pipe *pipe = grinder->pipe;
1888 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
1889 uint32_t tc_index = grinder->tc_index;
1890 uint32_t qmask = grinder->qmask;
1893 qindex = tc_index * 4;
1895 grinder->wrr_tokens[0] = ((uint16_t) pipe->wrr_tokens[qindex]) << RTE_SCHED_WRR_SHIFT;
1896 grinder->wrr_tokens[1] = ((uint16_t) pipe->wrr_tokens[qindex + 1]) << RTE_SCHED_WRR_SHIFT;
1897 grinder->wrr_tokens[2] = ((uint16_t) pipe->wrr_tokens[qindex + 2]) << RTE_SCHED_WRR_SHIFT;
1898 grinder->wrr_tokens[3] = ((uint16_t) pipe->wrr_tokens[qindex + 3]) << RTE_SCHED_WRR_SHIFT;
1900 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
1901 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
1902 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
1903 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
1905 grinder->wrr_cost[0] = pipe_params->wrr_cost[qindex];
1906 grinder->wrr_cost[1] = pipe_params->wrr_cost[qindex + 1];
1907 grinder->wrr_cost[2] = pipe_params->wrr_cost[qindex + 2];
1908 grinder->wrr_cost[3] = pipe_params->wrr_cost[qindex + 3];
1912 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
1914 struct rte_sched_grinder *grinder = port->grinder + pos;
1915 struct rte_sched_pipe *pipe = grinder->pipe;
1916 uint32_t tc_index = grinder->tc_index;
1919 qindex = tc_index * 4;
1921 pipe->wrr_tokens[qindex] = (grinder->wrr_tokens[0] & grinder->wrr_mask[0])
1922 >> RTE_SCHED_WRR_SHIFT;
1923 pipe->wrr_tokens[qindex + 1] = (grinder->wrr_tokens[1] & grinder->wrr_mask[1])
1924 >> RTE_SCHED_WRR_SHIFT;
1925 pipe->wrr_tokens[qindex + 2] = (grinder->wrr_tokens[2] & grinder->wrr_mask[2])
1926 >> RTE_SCHED_WRR_SHIFT;
1927 pipe->wrr_tokens[qindex + 3] = (grinder->wrr_tokens[3] & grinder->wrr_mask[3])
1928 >> RTE_SCHED_WRR_SHIFT;
1932 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
1934 struct rte_sched_grinder *grinder = port->grinder + pos;
1935 uint16_t wrr_tokens_min;
1937 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
1938 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
1939 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
1940 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
1942 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
1943 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
1945 grinder->wrr_tokens[0] -= wrr_tokens_min;
1946 grinder->wrr_tokens[1] -= wrr_tokens_min;
1947 grinder->wrr_tokens[2] -= wrr_tokens_min;
1948 grinder->wrr_tokens[3] -= wrr_tokens_min;
1952 #define grinder_evict(port, pos)
1955 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
1957 struct rte_sched_grinder *grinder = port->grinder + pos;
1959 rte_prefetch0(grinder->pipe);
1960 rte_prefetch0(grinder->queue[0]);
1964 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
1966 struct rte_sched_grinder *grinder = port->grinder + pos;
1967 uint16_t qsize, qr[4];
1969 qsize = grinder->qsize;
1970 qr[0] = grinder->queue[0]->qr & (qsize - 1);
1971 qr[1] = grinder->queue[1]->qr & (qsize - 1);
1972 qr[2] = grinder->queue[2]->qr & (qsize - 1);
1973 qr[3] = grinder->queue[3]->qr & (qsize - 1);
1975 rte_prefetch0(grinder->qbase[0] + qr[0]);
1976 rte_prefetch0(grinder->qbase[1] + qr[1]);
1978 grinder_wrr_load(port, pos);
1979 grinder_wrr(port, pos);
1981 rte_prefetch0(grinder->qbase[2] + qr[2]);
1982 rte_prefetch0(grinder->qbase[3] + qr[3]);
1986 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
1988 struct rte_sched_grinder *grinder = port->grinder + pos;
1989 uint32_t qpos = grinder->qpos;
1990 struct rte_mbuf **qbase = grinder->qbase[qpos];
1991 uint16_t qsize = grinder->qsize;
1992 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
1994 grinder->pkt = qbase[qr];
1995 rte_prefetch0(grinder->pkt);
1997 if (unlikely((qr & 0x7) == 7)) {
1998 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2000 rte_prefetch0(qbase + qr_next);
2004 static inline uint32_t
2005 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2007 struct rte_sched_grinder *grinder = port->grinder + pos;
2009 switch (grinder->state) {
2010 case e_GRINDER_PREFETCH_PIPE:
2012 if (grinder_next_pipe(port, pos)) {
2013 grinder_prefetch_pipe(port, pos);
2014 port->busy_grinders++;
2016 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2023 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2025 struct rte_sched_pipe *pipe = grinder->pipe;
2027 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2028 grinder_prefetch_tc_queue_arrays(port, pos);
2029 grinder_credits_update(port, pos);
2031 grinder->state = e_GRINDER_PREFETCH_MBUF;
2035 case e_GRINDER_PREFETCH_MBUF:
2037 grinder_prefetch_mbuf(port, pos);
2039 grinder->state = e_GRINDER_READ_MBUF;
2043 case e_GRINDER_READ_MBUF:
2045 uint32_t result = 0;
2047 result = grinder_schedule(port, pos);
2049 /* Look for next packet within the same TC */
2050 if (result && grinder->qmask) {
2051 grinder_wrr(port, pos);
2052 grinder_prefetch_mbuf(port, pos);
2056 grinder_wrr_store(port, pos);
2058 /* Look for another active TC within same pipe */
2059 if (grinder_next_tc(port, pos)) {
2060 grinder_prefetch_tc_queue_arrays(port, pos);
2062 grinder->state = e_GRINDER_PREFETCH_MBUF;
2066 if (grinder->productive == 0 &&
2067 port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2068 port->pipe_loop = grinder->pindex;
2070 grinder_evict(port, pos);
2072 /* Look for another active pipe */
2073 if (grinder_next_pipe(port, pos)) {
2074 grinder_prefetch_pipe(port, pos);
2076 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2080 /* No active pipe found */
2081 port->busy_grinders--;
2083 grinder->state = e_GRINDER_PREFETCH_PIPE;
2088 rte_panic("Algorithmic error (invalid state)\n");
2094 rte_sched_port_time_resync(struct rte_sched_port *port)
2096 uint64_t cycles = rte_get_tsc_cycles();
2097 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2098 double bytes_diff = ((double) cycles_diff) / port->cycles_per_byte;
2100 /* Advance port time */
2101 port->time_cpu_cycles = cycles;
2102 port->time_cpu_bytes += (uint64_t) bytes_diff;
2103 if (port->time < port->time_cpu_bytes)
2104 port->time = port->time_cpu_bytes;
2106 /* Reset pipe loop detection */
2107 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2111 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2115 /* Check if any exception flag is set */
2116 exceptions = (second_pass && port->busy_grinders == 0) ||
2117 (port->pipe_exhaustion == 1);
2119 /* Clear exception flags */
2120 port->pipe_exhaustion = 0;
2126 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2130 port->pkts_out = pkts;
2131 port->n_pkts_out = 0;
2133 rte_sched_port_time_resync(port);
2135 /* Take each queue in the grinder one step further */
2136 for (i = 0, count = 0; ; i++) {
2137 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2138 if ((count == n_pkts) ||
2139 rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {