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,
188 * Path through the scheduler hierarchy used by the scheduler enqueue
189 * operation to identify the destination queue for the current
190 * packet. Stored in the field pkt.hash.sched of struct rte_mbuf of
191 * each packet, typically written by the classification stage and read
192 * by scheduler enqueue.
194 struct __rte_sched_port_hierarchy {
195 uint32_t queue:2; /**< Queue ID (0 .. 3) */
196 uint32_t traffic_class:2; /**< Traffic class ID (0 .. 3)*/
197 uint32_t pipe:20; /**< Pipe ID */
198 uint32_t subport:6; /**< Subport ID */
199 uint32_t color:2; /**< Color */
202 struct rte_sched_grinder {
204 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
205 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
210 enum grinder_state state;
213 struct rte_sched_subport *subport;
214 struct rte_sched_pipe *pipe;
215 struct rte_sched_pipe_profile *pipe_params;
218 uint8_t tccache_qmask[4];
219 uint32_t tccache_qindex[4];
225 struct rte_sched_queue *queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
226 struct rte_mbuf **qbase[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
227 uint32_t qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
231 struct rte_mbuf *pkt;
234 uint16_t wrr_tokens[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
235 uint16_t wrr_mask[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
236 uint8_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
239 struct rte_sched_port {
240 /* User parameters */
241 uint32_t n_subports_per_port;
242 uint32_t n_pipes_per_subport;
245 uint32_t frame_overhead;
246 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
247 uint32_t n_pipe_profiles;
248 uint32_t pipe_tc3_rate_max;
250 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][e_RTE_METER_COLORS];
254 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cyles */
255 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
256 uint64_t time; /* Current NIC TX time measured in bytes */
257 double cycles_per_byte; /* CPU cycles per byte */
259 /* Scheduling loop detection */
261 uint32_t pipe_exhaustion;
264 struct rte_bitmap *bmp;
265 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
268 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
269 uint32_t busy_grinders;
270 struct rte_mbuf **pkts_out;
273 /* Queue base calculation */
274 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
277 /* Large data structures */
278 struct rte_sched_subport *subport;
279 struct rte_sched_pipe *pipe;
280 struct rte_sched_queue *queue;
281 struct rte_sched_queue_extra *queue_extra;
282 struct rte_sched_pipe_profile *pipe_profiles;
284 struct rte_mbuf **queue_array;
285 uint8_t memory[0] __rte_cache_aligned;
286 } __rte_cache_aligned;
288 enum rte_sched_port_array {
289 e_RTE_SCHED_PORT_ARRAY_SUBPORT = 0,
290 e_RTE_SCHED_PORT_ARRAY_PIPE,
291 e_RTE_SCHED_PORT_ARRAY_QUEUE,
292 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA,
293 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES,
294 e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY,
295 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY,
296 e_RTE_SCHED_PORT_ARRAY_TOTAL,
299 #ifdef RTE_SCHED_COLLECT_STATS
301 static inline uint32_t
302 rte_sched_port_queues_per_subport(struct rte_sched_port *port)
304 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport;
309 static inline uint32_t
310 rte_sched_port_queues_per_port(struct rte_sched_port *port)
312 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport * port->n_subports_per_port;
316 rte_sched_port_check_params(struct rte_sched_port_params *params)
320 if (params == NULL) {
325 if ((params->socket < 0) || (params->socket >= RTE_MAX_NUMA_NODES)) {
330 if (params->rate == 0) {
335 if (params->mtu == 0) {
339 /* n_subports_per_port: non-zero, power of 2 */
340 if ((params->n_subports_per_port == 0) || (!rte_is_power_of_2(params->n_subports_per_port))) {
344 /* n_pipes_per_subport: non-zero, power of 2 */
345 if ((params->n_pipes_per_subport == 0) || (!rte_is_power_of_2(params->n_pipes_per_subport))) {
349 /* qsize: non-zero, power of 2, no bigger than 32K (due to 16-bit read/write pointers) */
350 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
351 uint16_t qsize = params->qsize[i];
353 if ((qsize == 0) || (!rte_is_power_of_2(qsize))) {
358 /* pipe_profiles and n_pipe_profiles */
359 if ((params->pipe_profiles == NULL) ||
360 (params->n_pipe_profiles == 0) ||
361 (params->n_pipe_profiles > RTE_SCHED_PIPE_PROFILES_PER_PORT)) {
365 for (i = 0; i < params->n_pipe_profiles; i ++) {
366 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
368 /* TB rate: non-zero, not greater than port rate */
369 if ((p->tb_rate == 0) || (p->tb_rate > params->rate)) {
373 /* TB size: non-zero */
374 if (p->tb_size == 0) {
378 /* TC rate: non-zero, less than pipe rate */
379 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j ++) {
380 if ((p->tc_rate[j] == 0) || (p->tc_rate[j] > p->tb_rate)) {
385 /* TC period: non-zero */
386 if (p->tc_period == 0) {
390 #ifdef RTE_SCHED_SUBPORT_TC_OV
391 /* TC3 oversubscription weight: non-zero */
392 if (p->tc_ov_weight == 0) {
397 /* Queue WRR weights: non-zero */
398 for (j = 0; j < RTE_SCHED_QUEUES_PER_PIPE; j ++) {
399 if (p->wrr_weights[j] == 0) {
409 rte_sched_port_get_array_base(struct rte_sched_port_params *params, enum rte_sched_port_array array)
411 uint32_t n_subports_per_port = params->n_subports_per_port;
412 uint32_t n_pipes_per_subport = params->n_pipes_per_subport;
413 uint32_t n_pipes_per_port = n_pipes_per_subport * n_subports_per_port;
414 uint32_t n_queues_per_port = RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport * n_subports_per_port;
416 uint32_t size_subport = n_subports_per_port * sizeof(struct rte_sched_subport);
417 uint32_t size_pipe = n_pipes_per_port * sizeof(struct rte_sched_pipe);
418 uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue);
419 uint32_t size_queue_extra = n_queues_per_port * sizeof(struct rte_sched_queue_extra);
420 uint32_t size_pipe_profiles = RTE_SCHED_PIPE_PROFILES_PER_PORT * sizeof(struct rte_sched_pipe_profile);
421 uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port);
422 uint32_t size_per_pipe_queue_array, size_queue_array;
426 size_per_pipe_queue_array = 0;
427 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
428 size_per_pipe_queue_array += RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS * params->qsize[i] * sizeof(struct rte_mbuf *);
430 size_queue_array = n_pipes_per_port * size_per_pipe_queue_array;
434 if (array == e_RTE_SCHED_PORT_ARRAY_SUBPORT) return base;
435 base += RTE_CACHE_LINE_ROUNDUP(size_subport);
437 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE) return base;
438 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
440 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE) return base;
441 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
443 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA) return base;
444 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
446 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES) return base;
447 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
449 if (array == e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY) return base;
450 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
452 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY) return base;
453 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
459 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
461 uint32_t size0, size1;
464 status = rte_sched_port_check_params(params);
466 RTE_LOG(NOTICE, SCHED,
467 "Port scheduler params check failed (%d)\n", status);
472 size0 = sizeof(struct rte_sched_port);
473 size1 = rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_TOTAL);
475 return (size0 + size1);
479 rte_sched_port_config_qsize(struct rte_sched_port *port)
482 port->qsize_add[0] = 0;
483 port->qsize_add[1] = port->qsize_add[0] + port->qsize[0];
484 port->qsize_add[2] = port->qsize_add[1] + port->qsize[0];
485 port->qsize_add[3] = port->qsize_add[2] + port->qsize[0];
488 port->qsize_add[4] = port->qsize_add[3] + port->qsize[0];
489 port->qsize_add[5] = port->qsize_add[4] + port->qsize[1];
490 port->qsize_add[6] = port->qsize_add[5] + port->qsize[1];
491 port->qsize_add[7] = port->qsize_add[6] + port->qsize[1];
494 port->qsize_add[8] = port->qsize_add[7] + port->qsize[1];
495 port->qsize_add[9] = port->qsize_add[8] + port->qsize[2];
496 port->qsize_add[10] = port->qsize_add[9] + port->qsize[2];
497 port->qsize_add[11] = port->qsize_add[10] + port->qsize[2];
500 port->qsize_add[12] = port->qsize_add[11] + port->qsize[2];
501 port->qsize_add[13] = port->qsize_add[12] + port->qsize[3];
502 port->qsize_add[14] = port->qsize_add[13] + port->qsize[3];
503 port->qsize_add[15] = port->qsize_add[14] + port->qsize[3];
505 port->qsize_sum = port->qsize_add[15] + port->qsize[3];
509 rte_sched_port_log_pipe_profile(struct rte_sched_port *port, uint32_t i)
511 struct rte_sched_pipe_profile *p = port->pipe_profiles + i;
513 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
514 " Token bucket: period = %u, credits per period = %u, size = %u\n"
515 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
516 " Traffic class 3 oversubscription: weight = %hhu\n"
517 " WRR cost: [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu]\n",
522 p->tb_credits_per_period,
525 /* Traffic classes */
527 p->tc_credits_per_period[0],
528 p->tc_credits_per_period[1],
529 p->tc_credits_per_period[2],
530 p->tc_credits_per_period[3],
532 /* Traffic class 3 oversubscription */
536 p->wrr_cost[ 0], p->wrr_cost[ 1], p->wrr_cost[ 2], p->wrr_cost[ 3],
537 p->wrr_cost[ 4], p->wrr_cost[ 5], p->wrr_cost[ 6], p->wrr_cost[ 7],
538 p->wrr_cost[ 8], p->wrr_cost[ 9], p->wrr_cost[10], p->wrr_cost[11],
539 p->wrr_cost[12], p->wrr_cost[13], p->wrr_cost[14], p->wrr_cost[15]);
542 static inline uint64_t
543 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
545 uint64_t time = time_ms;
546 time = (time * rate) / 1000;
552 rte_sched_port_config_pipe_profile_table(struct rte_sched_port *port, struct rte_sched_port_params *params)
556 for (i = 0; i < port->n_pipe_profiles; i ++) {
557 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
558 struct rte_sched_pipe_profile *dst = port->pipe_profiles + i;
561 if (src->tb_rate == params->rate) {
562 dst->tb_credits_per_period = 1;
565 double tb_rate = ((double) src->tb_rate) / ((double) params->rate);
566 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
568 rte_approx(tb_rate, d, &dst->tb_credits_per_period, &dst->tb_period);
570 dst->tb_size = src->tb_size;
572 /* Traffic Classes */
573 dst->tc_period = (uint32_t) rte_sched_time_ms_to_bytes(src->tc_period, params->rate);
574 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j ++) {
575 dst->tc_credits_per_period[j] = (uint32_t) rte_sched_time_ms_to_bytes(src->tc_period, src->tc_rate[j]);
577 #ifdef RTE_SCHED_SUBPORT_TC_OV
578 dst->tc_ov_weight = src->tc_ov_weight;
582 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j ++) {
583 uint32_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
584 uint32_t lcd, lcd1, lcd2;
587 qindex = j * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS;
589 wrr_cost[0] = src->wrr_weights[qindex];
590 wrr_cost[1] = src->wrr_weights[qindex + 1];
591 wrr_cost[2] = src->wrr_weights[qindex + 2];
592 wrr_cost[3] = src->wrr_weights[qindex + 3];
594 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
595 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
596 lcd = rte_get_lcd(lcd1, lcd2);
598 wrr_cost[0] = lcd / wrr_cost[0];
599 wrr_cost[1] = lcd / wrr_cost[1];
600 wrr_cost[2] = lcd / wrr_cost[2];
601 wrr_cost[3] = lcd / wrr_cost[3];
603 dst->wrr_cost[qindex] = (uint8_t) wrr_cost[0];
604 dst->wrr_cost[qindex + 1] = (uint8_t) wrr_cost[1];
605 dst->wrr_cost[qindex + 2] = (uint8_t) wrr_cost[2];
606 dst->wrr_cost[qindex + 3] = (uint8_t) wrr_cost[3];
609 rte_sched_port_log_pipe_profile(port, i);
612 port->pipe_tc3_rate_max = 0;
613 for (i = 0; i < port->n_pipe_profiles; i ++) {
614 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
615 uint32_t pipe_tc3_rate = src->tc_rate[3];
617 if (port->pipe_tc3_rate_max < pipe_tc3_rate) {
618 port->pipe_tc3_rate_max = pipe_tc3_rate;
623 struct rte_sched_port *
624 rte_sched_port_config(struct rte_sched_port_params *params)
626 struct rte_sched_port *port = NULL;
627 uint32_t mem_size, bmp_mem_size, n_queues_per_port, i;
629 /* Check user parameters. Determine the amount of memory to allocate */
630 mem_size = rte_sched_port_get_memory_footprint(params);
635 /* Allocate memory to store the data structures */
636 port = rte_zmalloc("qos_params", mem_size, RTE_CACHE_LINE_SIZE);
641 /* User parameters */
642 port->n_subports_per_port = params->n_subports_per_port;
643 port->n_pipes_per_subport = params->n_pipes_per_subport;
644 port->rate = params->rate;
645 port->mtu = params->mtu + params->frame_overhead;
646 port->frame_overhead = params->frame_overhead;
647 memcpy(port->qsize, params->qsize, sizeof(params->qsize));
648 port->n_pipe_profiles = params->n_pipe_profiles;
651 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
654 for (j = 0; j < e_RTE_METER_COLORS; j++) {
655 /* if min/max are both zero, then RED is disabled */
656 if ((params->red_params[i][j].min_th |
657 params->red_params[i][j].max_th) == 0) {
661 if (rte_red_config_init(&port->red_config[i][j],
662 params->red_params[i][j].wq_log2,
663 params->red_params[i][j].min_th,
664 params->red_params[i][j].max_th,
665 params->red_params[i][j].maxp_inv) != 0) {
673 port->time_cpu_cycles = rte_get_tsc_cycles();
674 port->time_cpu_bytes = 0;
676 port->cycles_per_byte = ((double) rte_get_tsc_hz()) / ((double) params->rate);
678 /* Scheduling loop detection */
679 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
680 port->pipe_exhaustion = 0;
683 port->busy_grinders = 0;
684 port->pkts_out = NULL;
685 port->n_pkts_out = 0;
687 /* Queue base calculation */
688 rte_sched_port_config_qsize(port);
690 /* Large data structures */
691 port->subport = (struct rte_sched_subport *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_SUBPORT));
692 port->pipe = (struct rte_sched_pipe *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_PIPE));
693 port->queue = (struct rte_sched_queue *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_QUEUE));
694 port->queue_extra = (struct rte_sched_queue_extra *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA));
695 port->pipe_profiles = (struct rte_sched_pipe_profile *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES));
696 port->bmp_array = port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY);
697 port->queue_array = (struct rte_mbuf **) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY));
699 /* Pipe profile table */
700 rte_sched_port_config_pipe_profile_table(port, params);
703 n_queues_per_port = rte_sched_port_queues_per_port(port);
704 bmp_mem_size = rte_bitmap_get_memory_footprint(n_queues_per_port);
705 port->bmp = rte_bitmap_init(n_queues_per_port, port->bmp_array, bmp_mem_size);
706 if (port->bmp == NULL) {
707 RTE_LOG(ERR, SCHED, "Bitmap init error\n");
710 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i ++) {
711 port->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
718 rte_sched_port_free(struct rte_sched_port *port)
720 /* Check user parameters */
725 rte_bitmap_free(port->bmp);
730 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
732 struct rte_sched_subport *s = port->subport + i;
734 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
735 " Token bucket: period = %u, credits per period = %u, size = %u\n"
736 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
737 " Traffic class 3 oversubscription: wm min = %u, wm max = %u\n",
742 s->tb_credits_per_period,
745 /* Traffic classes */
747 s->tc_credits_per_period[0],
748 s->tc_credits_per_period[1],
749 s->tc_credits_per_period[2],
750 s->tc_credits_per_period[3],
752 /* Traffic class 3 oversubscription */
758 rte_sched_subport_config(struct rte_sched_port *port,
760 struct rte_sched_subport_params *params)
762 struct rte_sched_subport *s;
765 /* Check user parameters */
766 if ((port == NULL) ||
767 (subport_id >= port->n_subports_per_port) ||
772 if ((params->tb_rate == 0) || (params->tb_rate > port->rate)) {
776 if (params->tb_size == 0) {
780 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
781 if ((params->tc_rate[i] == 0) || (params->tc_rate[i] > params->tb_rate)) {
786 if (params->tc_period == 0) {
790 s = port->subport + subport_id;
792 /* Token Bucket (TB) */
793 if (params->tb_rate == port->rate) {
794 s->tb_credits_per_period = 1;
797 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
798 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
800 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
802 s->tb_size = params->tb_size;
803 s->tb_time = port->time;
804 s->tb_credits = s->tb_size / 2;
806 /* Traffic Classes (TCs) */
807 s->tc_period = (uint32_t) rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
808 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
809 s->tc_credits_per_period[i] = (uint32_t) rte_sched_time_ms_to_bytes(params->tc_period, params->tc_rate[i]);
811 s->tc_time = port->time + s->tc_period;
812 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
813 s->tc_credits[i] = s->tc_credits_per_period[i];
816 #ifdef RTE_SCHED_SUBPORT_TC_OV
817 /* TC oversubscription */
818 s->tc_ov_wm_min = port->mtu;
819 s->tc_ov_wm_max = (uint32_t) rte_sched_time_ms_to_bytes(params->tc_period, port->pipe_tc3_rate_max);
820 s->tc_ov_wm = s->tc_ov_wm_max;
821 s->tc_ov_period_id = 0;
827 rte_sched_port_log_subport_config(port, subport_id);
833 rte_sched_pipe_config(struct rte_sched_port *port,
836 int32_t pipe_profile)
838 struct rte_sched_subport *s;
839 struct rte_sched_pipe *p;
840 struct rte_sched_pipe_profile *params;
841 uint32_t deactivate, profile, i;
843 /* Check user parameters */
844 profile = (uint32_t) pipe_profile;
845 deactivate = (pipe_profile < 0);
846 if ((port == NULL) ||
847 (subport_id >= port->n_subports_per_port) ||
848 (pipe_id >= port->n_pipes_per_subport) ||
849 ((!deactivate) && (profile >= port->n_pipe_profiles))) {
853 /* Check that subport configuration is valid */
854 s = port->subport + subport_id;
855 if (s->tb_period == 0) {
859 p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
861 /* Handle the case when pipe already has a valid configuration */
863 params = port->pipe_profiles + p->profile;
865 #ifdef RTE_SCHED_SUBPORT_TC_OV
866 double subport_tc3_rate = ((double) s->tc_credits_per_period[3]) / ((double) s->tc_period);
867 double pipe_tc3_rate = ((double) params->tc_credits_per_period[3]) / ((double) params->tc_period);
868 uint32_t tc3_ov = s->tc_ov;
870 /* Unplug pipe from its subport */
871 s->tc_ov_n -= params->tc_ov_weight;
872 s->tc_ov_rate -= pipe_tc3_rate;
873 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
875 if (s->tc_ov != tc3_ov) {
876 RTE_LOG(DEBUG, SCHED,
877 "Subport %u TC3 oversubscription is OFF (%.4lf >= %.4lf)\n",
878 subport_id, subport_tc3_rate, s->tc_ov_rate);
883 memset(p, 0, sizeof(struct rte_sched_pipe));
890 /* Apply the new pipe configuration */
891 p->profile = profile;
892 params = port->pipe_profiles + p->profile;
894 /* Token Bucket (TB) */
895 p->tb_time = port->time;
896 p->tb_credits = params->tb_size / 2;
898 /* Traffic Classes (TCs) */
899 p->tc_time = port->time + params->tc_period;
900 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
901 p->tc_credits[i] = params->tc_credits_per_period[i];
904 #ifdef RTE_SCHED_SUBPORT_TC_OV
906 /* Subport TC3 oversubscription */
907 double subport_tc3_rate = ((double) s->tc_credits_per_period[3]) / ((double) s->tc_period);
908 double pipe_tc3_rate = ((double) params->tc_credits_per_period[3]) / ((double) params->tc_period);
909 uint32_t tc3_ov = s->tc_ov;
911 s->tc_ov_n += params->tc_ov_weight;
912 s->tc_ov_rate += pipe_tc3_rate;
913 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
915 if (s->tc_ov != tc3_ov) {
916 RTE_LOG(DEBUG, SCHED,
917 "Subport %u TC3 oversubscription is ON (%.4lf < %.4lf)\n",
918 subport_id, subport_tc3_rate, s->tc_ov_rate);
920 p->tc_ov_period_id = s->tc_ov_period_id;
921 p->tc_ov_credits = s->tc_ov_wm;
929 rte_sched_port_pkt_write(struct rte_mbuf *pkt,
930 uint32_t subport, uint32_t pipe, uint32_t traffic_class,
931 uint32_t queue, enum rte_meter_color color)
933 struct __rte_sched_port_hierarchy *sched
934 = (struct __rte_sched_port_hierarchy *) &pkt->hash.sched;
936 sched->color = (uint32_t) color;
937 sched->subport = subport;
939 sched->traffic_class = traffic_class;
940 sched->queue = queue;
944 rte_sched_port_pkt_read_tree_path(const struct rte_mbuf *pkt,
945 uint32_t *subport, uint32_t *pipe,
946 uint32_t *traffic_class, uint32_t *queue)
948 const struct __rte_sched_port_hierarchy *sched
949 = (const struct __rte_sched_port_hierarchy *) &pkt->hash.sched;
951 *subport = sched->subport;
953 *traffic_class = sched->traffic_class;
954 *queue = sched->queue;
959 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
961 const struct __rte_sched_port_hierarchy *sched
962 = (const struct __rte_sched_port_hierarchy *) &pkt->hash.sched;
964 return (enum rte_meter_color) sched->color;
968 rte_sched_subport_read_stats(struct rte_sched_port *port,
970 struct rte_sched_subport_stats *stats,
973 struct rte_sched_subport *s;
975 /* Check user parameters */
976 if ((port == NULL) ||
977 (subport_id >= port->n_subports_per_port) ||
982 s = port->subport + subport_id;
984 /* Copy subport stats and clear */
985 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
986 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
988 /* Subport TC ovesubscription status */
995 rte_sched_queue_read_stats(struct rte_sched_port *port,
997 struct rte_sched_queue_stats *stats,
1000 struct rte_sched_queue *q;
1001 struct rte_sched_queue_extra *qe;
1003 /* Check user parameters */
1004 if ((port == NULL) ||
1005 (queue_id >= rte_sched_port_queues_per_port(port)) ||
1010 q = port->queue + queue_id;
1011 qe = port->queue_extra + queue_id;
1013 /* Copy queue stats and clear */
1014 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1015 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1018 *qlen = q->qw - q->qr;
1023 static inline uint32_t
1024 rte_sched_port_qindex(struct rte_sched_port *port, uint32_t subport, uint32_t pipe, uint32_t traffic_class, uint32_t queue)
1028 result = subport * port->n_pipes_per_subport + pipe;
1029 result = result * RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE + traffic_class;
1030 result = result * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
1035 static inline struct rte_mbuf **
1036 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
1038 uint32_t pindex = qindex >> 4;
1039 uint32_t qpos = qindex & 0xF;
1041 return (port->queue_array + pindex * port->qsize_sum + port->qsize_add[qpos]);
1044 static inline uint16_t
1045 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
1047 uint32_t tc = (qindex >> 2) & 0x3;
1049 return port->qsize[tc];
1055 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1057 struct rte_sched_queue *queue = port->queue + qindex;
1059 return (queue->qr == queue->qw);
1063 rte_sched_port_queue_is_full(struct rte_sched_port *port, uint32_t qindex)
1065 struct rte_sched_queue *queue = port->queue + qindex;
1066 uint16_t qsize = rte_sched_port_qsize(port, qindex);
1067 uint16_t qlen = queue->qw - queue->qr;
1069 return (qlen >= qsize);
1072 #endif /* RTE_SCHED_DEBUG */
1074 #ifdef RTE_SCHED_COLLECT_STATS
1077 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1079 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1080 uint32_t tc_index = (qindex >> 2) & 0x3;
1081 uint32_t pkt_len = pkt->pkt_len;
1083 s->stats.n_pkts_tc[tc_index] += 1;
1084 s->stats.n_bytes_tc[tc_index] += pkt_len;
1088 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1090 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1091 uint32_t tc_index = (qindex >> 2) & 0x3;
1092 uint32_t pkt_len = pkt->pkt_len;
1094 s->stats.n_pkts_tc_dropped[tc_index] += 1;
1095 s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1099 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1101 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1102 uint32_t pkt_len = pkt->pkt_len;
1104 qe->stats.n_pkts += 1;
1105 qe->stats.n_bytes += pkt_len;
1109 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1111 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1112 uint32_t pkt_len = pkt->pkt_len;
1114 qe->stats.n_pkts_dropped += 1;
1115 qe->stats.n_bytes_dropped += pkt_len;
1118 #endif /* RTE_SCHED_COLLECT_STATS */
1120 #ifdef RTE_SCHED_RED
1123 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1125 struct rte_sched_queue_extra *qe;
1126 struct rte_red_config *red_cfg;
1127 struct rte_red *red;
1129 enum rte_meter_color color;
1131 tc_index = (qindex >> 2) & 0x3;
1132 color = rte_sched_port_pkt_read_color(pkt);
1133 red_cfg = &port->red_config[tc_index][color];
1135 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1138 qe = port->queue_extra + qindex;
1141 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1145 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1147 struct rte_sched_queue_extra *qe;
1148 struct rte_red *red;
1150 qe = port->queue_extra + qindex;
1153 rte_red_mark_queue_empty(red, port->time);
1158 #define rte_sched_port_red_drop(port, pkt, qindex, qlen) 0
1160 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1162 #endif /* RTE_SCHED_RED */
1167 debug_pipe_is_empty(struct rte_sched_port *port, uint32_t pindex)
1171 qindex = pindex << 4;
1173 for (i = 0; i < 16; i ++){
1174 uint32_t queue_empty = rte_sched_port_queue_is_empty(port, qindex + i);
1175 uint32_t bmp_bit_clear = (rte_bitmap_get(port->bmp, qindex + i) == 0);
1177 if (queue_empty != bmp_bit_clear){
1178 rte_panic("Queue status mismatch for queue %u of pipe %u\n", i, pindex);
1190 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos, uint64_t bmp_slab)
1196 rte_panic("Empty slab at position %u\n", bmp_pos);
1200 for (i = 0, mask = 1; i < 64; i ++, mask <<= 1) {
1201 if (mask & bmp_slab){
1202 if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1203 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1210 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1215 #endif /* RTE_SCHED_DEBUG */
1217 static inline uint32_t
1218 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port, struct rte_mbuf *pkt)
1220 struct rte_sched_queue *q;
1221 #ifdef RTE_SCHED_COLLECT_STATS
1222 struct rte_sched_queue_extra *qe;
1224 uint32_t subport, pipe, traffic_class, queue, qindex;
1226 rte_sched_port_pkt_read_tree_path(pkt, &subport, &pipe, &traffic_class, &queue);
1228 qindex = rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1229 q = port->queue + qindex;
1231 #ifdef RTE_SCHED_COLLECT_STATS
1232 qe = port->queue_extra + qindex;
1240 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf **qbase)
1242 struct rte_sched_queue *q;
1243 struct rte_mbuf **q_qw;
1246 q = port->queue + qindex;
1247 qsize = rte_sched_port_qsize(port, qindex);
1248 q_qw = qbase + (q->qw & (qsize - 1));
1250 rte_prefetch0(q_qw);
1251 rte_bitmap_prefetch0(port->bmp, qindex);
1255 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1257 struct rte_sched_queue *q;
1261 q = port->queue + qindex;
1262 qsize = rte_sched_port_qsize(port, qindex);
1263 qlen = q->qw - q->qr;
1265 /* Drop the packet (and update drop stats) when queue is full */
1266 if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) || (qlen >= qsize))) {
1267 rte_pktmbuf_free(pkt);
1268 #ifdef RTE_SCHED_COLLECT_STATS
1269 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt);
1270 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt);
1275 /* Enqueue packet */
1276 qbase[q->qw & (qsize - 1)] = pkt;
1279 /* Activate queue in the port bitmap */
1280 rte_bitmap_set(port->bmp, qindex);
1283 #ifdef RTE_SCHED_COLLECT_STATS
1284 rte_sched_port_update_subport_stats(port, qindex, pkt);
1285 rte_sched_port_update_queue_stats(port, qindex, pkt);
1291 #if RTE_SCHED_ENQUEUE == 0
1294 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
1300 for (i = 0; i < n_pkts; i ++) {
1301 struct rte_mbuf *pkt;
1302 struct rte_mbuf **q_base;
1303 uint32_t subport, pipe, traffic_class, queue, qindex;
1307 rte_sched_port_pkt_read_tree_path(pkt, &subport, &pipe, &traffic_class, &queue);
1309 qindex = rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1311 q_base = rte_sched_port_qbase(port, qindex);
1313 result += rte_sched_port_enqueue_qwa(port, qindex, q_base, pkt);
1321 /* The enqueue function implements a 4-level pipeline with each stage processing
1322 * two different packets. The purpose of using a pipeline is to hide the latency
1323 * of prefetching the data structures. The naming convention is presented in the
1326 * p00 _______ p10 _______ p20 _______ p30 _______
1327 * ----->| |----->| |----->| |----->| |----->
1328 * | 0 | | 1 | | 2 | | 3 |
1329 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1334 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
1336 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21, *pkt30, *pkt31, *pkt_last;
1337 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base, **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1338 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1339 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1344 /* Less then 6 input packets available, which is not enough to feed the pipeline */
1345 if (unlikely(n_pkts < 6)) {
1346 struct rte_mbuf **q_base[5];
1349 /* Prefetch the mbuf structure of each packet */
1350 for (i = 0; i < n_pkts; i ++) {
1351 rte_prefetch0(pkts[i]);
1354 /* Prefetch the queue structure for each queue */
1355 for (i = 0; i < n_pkts; i ++) {
1356 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port, pkts[i]);
1359 /* Prefetch the write pointer location of each queue */
1360 for (i = 0; i < n_pkts; i ++) {
1361 q_base[i] = rte_sched_port_qbase(port, q[i]);
1362 rte_sched_port_enqueue_qwa_prefetch0(port, q[i], q_base[i]);
1365 /* Write each packet to its queue */
1366 for (i = 0; i < n_pkts; i ++) {
1367 result += rte_sched_port_enqueue_qwa(port, q[i], q_base[i], pkts[i]);
1373 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1376 rte_prefetch0(pkt20);
1377 rte_prefetch0(pkt21);
1381 rte_prefetch0(pkt10);
1382 rte_prefetch0(pkt11);
1384 q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1385 q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1389 rte_prefetch0(pkt00);
1390 rte_prefetch0(pkt01);
1392 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1393 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1395 q20_base = rte_sched_port_qbase(port, q20);
1396 q21_base = rte_sched_port_qbase(port, q21);
1397 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1398 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1400 /* Run the pipeline */
1401 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1402 /* Propagate stage inputs */
1413 q30_base = q20_base;
1414 q31_base = q21_base;
1416 /* Stage 0: Get packets in */
1418 pkt01 = pkts[i + 1];
1419 rte_prefetch0(pkt00);
1420 rte_prefetch0(pkt01);
1422 /* Stage 1: Prefetch queue structure storing queue pointers */
1423 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1424 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1426 /* Stage 2: Prefetch queue write location */
1427 q20_base = rte_sched_port_qbase(port, q20);
1428 q21_base = rte_sched_port_qbase(port, q21);
1429 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1430 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1432 /* Stage 3: Write packet to queue and activate queue */
1433 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1434 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1435 result += r30 + r31;
1438 /* Drain the pipeline (exactly 6 packets). Handle the last packet in the case
1439 of an odd number of input packets. */
1440 pkt_last = pkts[n_pkts - 1];
1441 rte_prefetch0(pkt_last);
1443 q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1444 q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1446 q10_base = rte_sched_port_qbase(port, q10);
1447 q11_base = rte_sched_port_qbase(port, q11);
1448 rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1449 rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1451 r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1452 r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1453 result += r20 + r21;
1455 q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1457 q00_base = rte_sched_port_qbase(port, q00);
1458 q01_base = rte_sched_port_qbase(port, q01);
1459 rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1460 rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1462 r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1463 r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1464 result += r10 + r11;
1466 q_last_base = rte_sched_port_qbase(port, q_last);
1467 rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1469 r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1470 r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1471 result += r00 + r01;
1474 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1481 #endif /* RTE_SCHED_ENQUEUE */
1483 #if RTE_SCHED_TS_CREDITS_UPDATE == 0
1485 #define grinder_credits_update(port, pos)
1487 #elif !defined(RTE_SCHED_SUBPORT_TC_OV)
1490 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1492 struct rte_sched_grinder *grinder = port->grinder + pos;
1493 struct rte_sched_subport *subport = grinder->subport;
1494 struct rte_sched_pipe *pipe = grinder->pipe;
1495 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1499 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1500 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1501 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1502 subport->tb_time += n_periods * subport->tb_period;
1505 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1506 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1507 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1508 pipe->tb_time += n_periods * params->tb_period;
1511 if (unlikely(port->time >= subport->tc_time)) {
1512 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1513 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1514 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1515 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1516 subport->tc_time = port->time + subport->tc_period;
1520 if (unlikely(port->time >= pipe->tc_time)) {
1521 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1522 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1523 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1524 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1525 pipe->tc_time = port->time + params->tc_period;
1531 static inline uint32_t
1532 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1534 struct rte_sched_grinder *grinder = port->grinder + pos;
1535 struct rte_sched_subport *subport = grinder->subport;
1536 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1537 uint32_t tc_ov_consumption_max;
1538 uint32_t tc_ov_wm = subport->tc_ov_wm;
1540 if (subport->tc_ov == 0) {
1541 return subport->tc_ov_wm_max;
1544 tc_ov_consumption[0] = subport->tc_credits_per_period[0] - subport->tc_credits[0];
1545 tc_ov_consumption[1] = subport->tc_credits_per_period[1] - subport->tc_credits[1];
1546 tc_ov_consumption[2] = subport->tc_credits_per_period[2] - subport->tc_credits[2];
1547 tc_ov_consumption[3] = subport->tc_credits_per_period[3] - subport->tc_credits[3];
1549 tc_ov_consumption_max = subport->tc_credits_per_period[3] -
1550 (tc_ov_consumption[0] + tc_ov_consumption[1] + tc_ov_consumption[2]);
1552 if (tc_ov_consumption[3] > (tc_ov_consumption_max - port->mtu)) {
1553 tc_ov_wm -= tc_ov_wm >> 7;
1554 if (tc_ov_wm < subport->tc_ov_wm_min) {
1555 tc_ov_wm = subport->tc_ov_wm_min;
1560 tc_ov_wm += (tc_ov_wm >> 7) + 1;
1561 if (tc_ov_wm > subport->tc_ov_wm_max) {
1562 tc_ov_wm = subport->tc_ov_wm_max;
1568 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1570 struct rte_sched_grinder *grinder = port->grinder + pos;
1571 struct rte_sched_subport *subport = grinder->subport;
1572 struct rte_sched_pipe *pipe = grinder->pipe;
1573 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1577 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1578 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1579 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1580 subport->tb_time += n_periods * subport->tb_period;
1583 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1584 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1585 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1586 pipe->tb_time += n_periods * params->tb_period;
1589 if (unlikely(port->time >= subport->tc_time)) {
1590 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
1592 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1593 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1594 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1595 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1597 subport->tc_time = port->time + subport->tc_period;
1598 subport->tc_ov_period_id ++;
1602 if (unlikely(port->time >= pipe->tc_time)) {
1603 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1604 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1605 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1606 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1607 pipe->tc_time = port->time + params->tc_period;
1610 /* Pipe TCs - Oversubscription */
1611 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
1612 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
1614 pipe->tc_ov_period_id = subport->tc_ov_period_id;
1618 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
1620 #if RTE_SCHED_TS_CREDITS_CHECK
1622 #ifndef RTE_SCHED_SUBPORT_TC_OV
1625 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1627 struct rte_sched_grinder *grinder = port->grinder + pos;
1628 struct rte_sched_subport *subport = grinder->subport;
1629 struct rte_sched_pipe *pipe = grinder->pipe;
1630 struct rte_mbuf *pkt = grinder->pkt;
1631 uint32_t tc_index = grinder->tc_index;
1632 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1633 uint32_t subport_tb_credits = subport->tb_credits;
1634 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1635 uint32_t pipe_tb_credits = pipe->tb_credits;
1636 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1639 /* Check queue credits */
1640 enough_credits = (pkt_len <= subport_tb_credits) &&
1641 (pkt_len <= subport_tc_credits) &&
1642 (pkt_len <= pipe_tb_credits) &&
1643 (pkt_len <= pipe_tc_credits);
1645 if (!enough_credits) {
1649 /* Update port credits */
1650 subport->tb_credits -= pkt_len;
1651 subport->tc_credits[tc_index] -= pkt_len;
1652 pipe->tb_credits -= pkt_len;
1653 pipe->tc_credits[tc_index] -= pkt_len;
1661 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1663 struct rte_sched_grinder *grinder = port->grinder + pos;
1664 struct rte_sched_subport *subport = grinder->subport;
1665 struct rte_sched_pipe *pipe = grinder->pipe;
1666 struct rte_mbuf *pkt = grinder->pkt;
1667 uint32_t tc_index = grinder->tc_index;
1668 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1669 uint32_t subport_tb_credits = subport->tb_credits;
1670 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1671 uint32_t pipe_tb_credits = pipe->tb_credits;
1672 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1673 uint32_t pipe_tc_ov_mask1[] = {UINT32_MAX, UINT32_MAX, UINT32_MAX, pipe->tc_ov_credits};
1674 uint32_t pipe_tc_ov_mask2[] = {0, 0, 0, UINT32_MAX};
1675 uint32_t pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
1678 /* Check pipe and subport credits */
1679 enough_credits = (pkt_len <= subport_tb_credits) &&
1680 (pkt_len <= subport_tc_credits) &&
1681 (pkt_len <= pipe_tb_credits) &&
1682 (pkt_len <= pipe_tc_credits) &&
1683 (pkt_len <= pipe_tc_ov_credits);
1685 if (!enough_credits) {
1689 /* Update pipe and subport credits */
1690 subport->tb_credits -= pkt_len;
1691 subport->tc_credits[tc_index] -= pkt_len;
1692 pipe->tb_credits -= pkt_len;
1693 pipe->tc_credits[tc_index] -= pkt_len;
1694 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
1699 #endif /* RTE_SCHED_SUBPORT_TC_OV */
1701 #endif /* RTE_SCHED_TS_CREDITS_CHECK */
1704 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
1706 struct rte_sched_grinder *grinder = port->grinder + pos;
1707 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
1708 struct rte_mbuf *pkt = grinder->pkt;
1709 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1711 #if RTE_SCHED_TS_CREDITS_CHECK
1712 if (!grinder_credits_check(port, pos)) {
1717 /* Advance port time */
1718 port->time += pkt_len;
1721 port->pkts_out[port->n_pkts_out ++] = pkt;
1723 grinder->wrr_tokens[grinder->qpos] += pkt_len * grinder->wrr_cost[grinder->qpos];
1724 if (queue->qr == queue->qw) {
1725 uint32_t qindex = grinder->qindex[grinder->qpos];
1727 rte_bitmap_clear(port->bmp, qindex);
1728 grinder->qmask &= ~(1 << grinder->qpos);
1729 grinder->wrr_mask[grinder->qpos] = 0;
1730 rte_sched_port_set_queue_empty_timestamp(port, qindex);
1733 /* Reset pipe loop detection */
1734 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1735 grinder->productive = 1;
1740 #if RTE_SCHED_OPTIMIZATIONS
1743 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1745 __m128i index = _mm_set1_epi32 (base_pipe);
1746 __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
1747 __m128i res = _mm_cmpeq_epi32(pipes, index);
1748 pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
1749 pipes = _mm_cmpeq_epi32(pipes, index);
1750 res = _mm_or_si128(res, pipes);
1752 if (_mm_testz_si128(res, res))
1761 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1765 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i ++) {
1766 if (port->grinder_base_bmp_pos[i] == base_pipe) {
1774 #endif /* RTE_SCHED_OPTIMIZATIONS */
1777 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
1779 struct rte_sched_grinder *grinder = port->grinder + pos;
1782 grinder->pcache_w = 0;
1783 grinder->pcache_r = 0;
1785 w[0] = (uint16_t) bmp_slab;
1786 w[1] = (uint16_t) (bmp_slab >> 16);
1787 w[2] = (uint16_t) (bmp_slab >> 32);
1788 w[3] = (uint16_t) (bmp_slab >> 48);
1790 grinder->pcache_qmask[grinder->pcache_w] = w[0];
1791 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
1792 grinder->pcache_w += (w[0] != 0);
1794 grinder->pcache_qmask[grinder->pcache_w] = w[1];
1795 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
1796 grinder->pcache_w += (w[1] != 0);
1798 grinder->pcache_qmask[grinder->pcache_w] = w[2];
1799 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
1800 grinder->pcache_w += (w[2] != 0);
1802 grinder->pcache_qmask[grinder->pcache_w] = w[3];
1803 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
1804 grinder->pcache_w += (w[3] != 0);
1808 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
1810 struct rte_sched_grinder *grinder = port->grinder + pos;
1813 grinder->tccache_w = 0;
1814 grinder->tccache_r = 0;
1816 b[0] = (uint8_t) (qmask & 0xF);
1817 b[1] = (uint8_t) ((qmask >> 4) & 0xF);
1818 b[2] = (uint8_t) ((qmask >> 8) & 0xF);
1819 b[3] = (uint8_t) ((qmask >> 12) & 0xF);
1821 grinder->tccache_qmask[grinder->tccache_w] = b[0];
1822 grinder->tccache_qindex[grinder->tccache_w] = qindex;
1823 grinder->tccache_w += (b[0] != 0);
1825 grinder->tccache_qmask[grinder->tccache_w] = b[1];
1826 grinder->tccache_qindex[grinder->tccache_w] = qindex + 4;
1827 grinder->tccache_w += (b[1] != 0);
1829 grinder->tccache_qmask[grinder->tccache_w] = b[2];
1830 grinder->tccache_qindex[grinder->tccache_w] = qindex + 8;
1831 grinder->tccache_w += (b[2] != 0);
1833 grinder->tccache_qmask[grinder->tccache_w] = b[3];
1834 grinder->tccache_qindex[grinder->tccache_w] = qindex + 12;
1835 grinder->tccache_w += (b[3] != 0);
1839 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
1841 struct rte_sched_grinder *grinder = port->grinder + pos;
1842 struct rte_mbuf **qbase;
1846 if (grinder->tccache_r == grinder->tccache_w) {
1850 qindex = grinder->tccache_qindex[grinder->tccache_r];
1851 qbase = rte_sched_port_qbase(port, qindex);
1852 qsize = rte_sched_port_qsize(port, qindex);
1854 grinder->tc_index = (qindex >> 2) & 0x3;
1855 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
1856 grinder->qsize = qsize;
1858 grinder->qindex[0] = qindex;
1859 grinder->qindex[1] = qindex + 1;
1860 grinder->qindex[2] = qindex + 2;
1861 grinder->qindex[3] = qindex + 3;
1863 grinder->queue[0] = port->queue + qindex;
1864 grinder->queue[1] = port->queue + qindex + 1;
1865 grinder->queue[2] = port->queue + qindex + 2;
1866 grinder->queue[3] = port->queue + qindex + 3;
1868 grinder->qbase[0] = qbase;
1869 grinder->qbase[1] = qbase + qsize;
1870 grinder->qbase[2] = qbase + 2 * qsize;
1871 grinder->qbase[3] = qbase + 3 * qsize;
1873 grinder->tccache_r ++;
1878 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
1880 struct rte_sched_grinder *grinder = port->grinder + pos;
1881 uint32_t pipe_qindex;
1882 uint16_t pipe_qmask;
1884 if (grinder->pcache_r < grinder->pcache_w) {
1885 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
1886 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
1887 grinder->pcache_r ++;
1889 uint64_t bmp_slab = 0;
1890 uint32_t bmp_pos = 0;
1892 /* Get another non-empty pipe group */
1893 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0)) {
1898 debug_check_queue_slab(port, bmp_pos, bmp_slab);
1901 /* Return if pipe group already in one of the other grinders */
1902 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
1903 if (unlikely(grinder_pipe_exists(port, bmp_pos))) {
1906 port->grinder_base_bmp_pos[pos] = bmp_pos;
1908 /* Install new pipe group into grinder's pipe cache */
1909 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
1911 pipe_qmask = grinder->pcache_qmask[0];
1912 pipe_qindex = grinder->pcache_qindex[0];
1913 grinder->pcache_r = 1;
1916 /* Install new pipe in the grinder */
1917 grinder->pindex = pipe_qindex >> 4;
1918 grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
1919 grinder->pipe = port->pipe + grinder->pindex;
1920 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
1921 grinder->productive = 0;
1923 grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
1924 grinder_next_tc(port, pos);
1926 /* Check for pipe exhaustion */
1927 if (grinder->pindex == port->pipe_loop) {
1928 port->pipe_exhaustion = 1;
1929 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1935 #if RTE_SCHED_WRR == 0
1937 #define grinder_wrr_load(a,b)
1939 #define grinder_wrr_store(a,b)
1942 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
1944 struct rte_sched_grinder *grinder = port->grinder + pos;
1945 uint64_t slab = grinder->qmask;
1947 if (rte_bsf64(slab, &grinder->qpos) == 0) {
1948 rte_panic("grinder wrr\n");
1952 #elif RTE_SCHED_WRR == 1
1955 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
1957 struct rte_sched_grinder *grinder = port->grinder + pos;
1958 struct rte_sched_pipe *pipe = grinder->pipe;
1959 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
1960 uint32_t tc_index = grinder->tc_index;
1961 uint32_t qmask = grinder->qmask;
1964 qindex = tc_index * 4;
1966 grinder->wrr_tokens[0] = ((uint16_t) pipe->wrr_tokens[qindex]) << RTE_SCHED_WRR_SHIFT;
1967 grinder->wrr_tokens[1] = ((uint16_t) pipe->wrr_tokens[qindex + 1]) << RTE_SCHED_WRR_SHIFT;
1968 grinder->wrr_tokens[2] = ((uint16_t) pipe->wrr_tokens[qindex + 2]) << RTE_SCHED_WRR_SHIFT;
1969 grinder->wrr_tokens[3] = ((uint16_t) pipe->wrr_tokens[qindex + 3]) << RTE_SCHED_WRR_SHIFT;
1971 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
1972 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
1973 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
1974 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
1976 grinder->wrr_cost[0] = pipe_params->wrr_cost[qindex];
1977 grinder->wrr_cost[1] = pipe_params->wrr_cost[qindex + 1];
1978 grinder->wrr_cost[2] = pipe_params->wrr_cost[qindex + 2];
1979 grinder->wrr_cost[3] = pipe_params->wrr_cost[qindex + 3];
1983 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
1985 struct rte_sched_grinder *grinder = port->grinder + pos;
1986 struct rte_sched_pipe *pipe = grinder->pipe;
1987 uint32_t tc_index = grinder->tc_index;
1990 qindex = tc_index * 4;
1992 pipe->wrr_tokens[qindex] = (uint8_t) ((grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >> RTE_SCHED_WRR_SHIFT);
1993 pipe->wrr_tokens[qindex + 1] = (uint8_t) ((grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >> RTE_SCHED_WRR_SHIFT);
1994 pipe->wrr_tokens[qindex + 2] = (uint8_t) ((grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >> RTE_SCHED_WRR_SHIFT);
1995 pipe->wrr_tokens[qindex + 3] = (uint8_t) ((grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >> RTE_SCHED_WRR_SHIFT);
1999 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
2001 struct rte_sched_grinder *grinder = port->grinder + pos;
2002 uint16_t wrr_tokens_min;
2004 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2005 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2006 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2007 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2009 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2010 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2012 grinder->wrr_tokens[0] -= wrr_tokens_min;
2013 grinder->wrr_tokens[1] -= wrr_tokens_min;
2014 grinder->wrr_tokens[2] -= wrr_tokens_min;
2015 grinder->wrr_tokens[3] -= wrr_tokens_min;
2020 #error Invalid value for RTE_SCHED_WRR
2022 #endif /* RTE_SCHED_WRR */
2024 #define grinder_evict(port, pos)
2027 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
2029 struct rte_sched_grinder *grinder = port->grinder + pos;
2031 rte_prefetch0(grinder->pipe);
2032 rte_prefetch0(grinder->queue[0]);
2036 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
2038 struct rte_sched_grinder *grinder = port->grinder + pos;
2039 uint16_t qsize, qr[4];
2041 qsize = grinder->qsize;
2042 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2043 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2044 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2045 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2047 rte_prefetch0(grinder->qbase[0] + qr[0]);
2048 rte_prefetch0(grinder->qbase[1] + qr[1]);
2050 grinder_wrr_load(port, pos);
2051 grinder_wrr(port, pos);
2053 rte_prefetch0(grinder->qbase[2] + qr[2]);
2054 rte_prefetch0(grinder->qbase[3] + qr[3]);
2058 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2060 struct rte_sched_grinder *grinder = port->grinder + pos;
2061 uint32_t qpos = grinder->qpos;
2062 struct rte_mbuf **qbase = grinder->qbase[qpos];
2063 uint16_t qsize = grinder->qsize;
2064 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2066 grinder->pkt = qbase[qr];
2067 rte_prefetch0(grinder->pkt);
2069 if (unlikely((qr & 0x7) == 7)) {
2070 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2072 rte_prefetch0(qbase + qr_next);
2076 static inline uint32_t
2077 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2079 struct rte_sched_grinder *grinder = port->grinder + pos;
2081 switch (grinder->state) {
2082 case e_GRINDER_PREFETCH_PIPE:
2084 if (grinder_next_pipe(port, pos)) {
2085 grinder_prefetch_pipe(port, pos);
2086 port->busy_grinders ++;
2088 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2095 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2097 struct rte_sched_pipe *pipe = grinder->pipe;
2099 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2100 grinder_prefetch_tc_queue_arrays(port, pos);
2101 grinder_credits_update(port, pos);
2103 grinder->state = e_GRINDER_PREFETCH_MBUF;
2107 case e_GRINDER_PREFETCH_MBUF:
2109 grinder_prefetch_mbuf(port, pos);
2111 grinder->state = e_GRINDER_READ_MBUF;
2115 case e_GRINDER_READ_MBUF:
2117 uint32_t result = 0;
2119 result = grinder_schedule(port, pos);
2121 /* Look for next packet within the same TC */
2122 if (result && grinder->qmask) {
2123 grinder_wrr(port, pos);
2124 grinder_prefetch_mbuf(port, pos);
2128 grinder_wrr_store(port, pos);
2130 /* Look for another active TC within same pipe */
2131 if (grinder_next_tc(port, pos)) {
2132 grinder_prefetch_tc_queue_arrays(port, pos);
2134 grinder->state = e_GRINDER_PREFETCH_MBUF;
2137 if ((grinder->productive == 0) && (port->pipe_loop == RTE_SCHED_PIPE_INVALID)) {
2138 port->pipe_loop = grinder->pindex;
2140 grinder_evict(port, pos);
2142 /* Look for another active pipe */
2143 if (grinder_next_pipe(port, pos)) {
2144 grinder_prefetch_pipe(port, pos);
2146 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2150 /* No active pipe found */
2151 port->busy_grinders --;
2153 grinder->state = e_GRINDER_PREFETCH_PIPE;
2158 rte_panic("Algorithmic error (invalid state)\n");
2164 rte_sched_port_time_resync(struct rte_sched_port *port)
2166 uint64_t cycles = rte_get_tsc_cycles();
2167 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2168 double bytes_diff = ((double) cycles_diff) / port->cycles_per_byte;
2170 /* Advance port time */
2171 port->time_cpu_cycles = cycles;
2172 port->time_cpu_bytes += (uint64_t) bytes_diff;
2173 if (port->time < port->time_cpu_bytes) {
2174 port->time = port->time_cpu_bytes;
2177 /* Reset pipe loop detection */
2178 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2182 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2186 /* Check if any exception flag is set */
2187 exceptions = (second_pass && port->busy_grinders == 0) ||
2188 (port->pipe_exhaustion == 1);
2190 /* Clear exception flags */
2191 port->pipe_exhaustion = 0;
2197 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2201 port->pkts_out = pkts;
2202 port->n_pkts_out = 0;
2204 rte_sched_port_time_resync(port);
2206 /* Take each queue in the grinder one step further */
2207 for (i = 0, count = 0; ; i ++) {
2208 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2209 if ((count == n_pkts) ||
2210 rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {