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"
50 #include "rte_reciprocal.h"
52 #ifdef __INTEL_COMPILER
53 #pragma warning(disable:2259) /* conversion may lose significant bits */
56 #ifdef RTE_SCHED_VECTOR
60 #define SCHED_VECTOR_SSE4
65 #define RTE_SCHED_TB_RATE_CONFIG_ERR (1e-7)
66 #define RTE_SCHED_WRR_SHIFT 3
67 #define RTE_SCHED_GRINDER_PCACHE_SIZE (64 / RTE_SCHED_QUEUES_PER_PIPE)
68 #define RTE_SCHED_PIPE_INVALID UINT32_MAX
69 #define RTE_SCHED_BMP_POS_INVALID UINT32_MAX
71 /* Scaling for cycles_per_byte calculation
72 * Chosen so that minimum rate is 480 bit/sec
74 #define RTE_SCHED_TIME_SHIFT 8
76 struct rte_sched_subport {
77 /* Token bucket (TB) */
78 uint64_t tb_time; /* time of last update */
80 uint32_t tb_credits_per_period;
84 /* Traffic classes (TCs) */
85 uint64_t tc_time; /* time of next update */
86 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
87 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
90 /* TC oversubscription */
92 uint32_t tc_ov_wm_min;
93 uint32_t tc_ov_wm_max;
94 uint8_t tc_ov_period_id;
100 struct rte_sched_subport_stats stats;
103 struct rte_sched_pipe_profile {
104 /* Token bucket (TB) */
106 uint32_t tb_credits_per_period;
109 /* Pipe traffic classes */
111 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
112 uint8_t tc_ov_weight;
115 uint8_t wrr_cost[RTE_SCHED_QUEUES_PER_PIPE];
118 struct rte_sched_pipe {
119 /* Token bucket (TB) */
120 uint64_t tb_time; /* time of last update */
123 /* Pipe profile and flags */
126 /* Traffic classes (TCs) */
127 uint64_t tc_time; /* time of next update */
128 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
130 /* Weighted Round Robin (WRR) */
131 uint8_t wrr_tokens[RTE_SCHED_QUEUES_PER_PIPE];
133 /* TC oversubscription */
134 uint32_t tc_ov_credits;
135 uint8_t tc_ov_period_id;
137 } __rte_cache_aligned;
139 struct rte_sched_queue {
144 struct rte_sched_queue_extra {
145 struct rte_sched_queue_stats stats;
152 e_GRINDER_PREFETCH_PIPE = 0,
153 e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
154 e_GRINDER_PREFETCH_MBUF,
159 * Path through the scheduler hierarchy used by the scheduler enqueue
160 * operation to identify the destination queue for the current
161 * packet. Stored in the field pkt.hash.sched of struct rte_mbuf of
162 * each packet, typically written by the classification stage and read
163 * by scheduler enqueue.
165 struct rte_sched_port_hierarchy {
166 uint16_t queue:2; /**< Queue ID (0 .. 3) */
167 uint16_t traffic_class:2; /**< Traffic class ID (0 .. 3)*/
168 uint32_t color:2; /**< Color */
170 uint16_t subport; /**< Subport ID */
171 uint32_t pipe; /**< Pipe ID */
174 struct rte_sched_grinder {
176 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
177 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
182 enum grinder_state state;
185 struct rte_sched_subport *subport;
186 struct rte_sched_pipe *pipe;
187 struct rte_sched_pipe_profile *pipe_params;
190 uint8_t tccache_qmask[4];
191 uint32_t tccache_qindex[4];
197 struct rte_sched_queue *queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
198 struct rte_mbuf **qbase[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
199 uint32_t qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
203 struct rte_mbuf *pkt;
206 uint16_t wrr_tokens[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
207 uint16_t wrr_mask[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
208 uint8_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
211 struct rte_sched_port {
212 /* User parameters */
213 uint32_t n_subports_per_port;
214 uint32_t n_pipes_per_subport;
217 uint32_t frame_overhead;
218 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
219 uint32_t n_pipe_profiles;
220 uint32_t pipe_tc3_rate_max;
222 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][e_RTE_METER_COLORS];
226 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cyles */
227 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
228 uint64_t time; /* Current NIC TX time measured in bytes */
229 struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
231 /* Scheduling loop detection */
233 uint32_t pipe_exhaustion;
236 struct rte_bitmap *bmp;
237 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
240 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
241 uint32_t busy_grinders;
242 struct rte_mbuf **pkts_out;
245 /* Queue base calculation */
246 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
249 /* Large data structures */
250 struct rte_sched_subport *subport;
251 struct rte_sched_pipe *pipe;
252 struct rte_sched_queue *queue;
253 struct rte_sched_queue_extra *queue_extra;
254 struct rte_sched_pipe_profile *pipe_profiles;
256 struct rte_mbuf **queue_array;
257 uint8_t memory[0] __rte_cache_aligned;
258 } __rte_cache_aligned;
260 enum rte_sched_port_array {
261 e_RTE_SCHED_PORT_ARRAY_SUBPORT = 0,
262 e_RTE_SCHED_PORT_ARRAY_PIPE,
263 e_RTE_SCHED_PORT_ARRAY_QUEUE,
264 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA,
265 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES,
266 e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY,
267 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY,
268 e_RTE_SCHED_PORT_ARRAY_TOTAL,
271 #ifdef RTE_SCHED_COLLECT_STATS
273 static inline uint32_t
274 rte_sched_port_queues_per_subport(struct rte_sched_port *port)
276 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport;
281 static inline uint32_t
282 rte_sched_port_queues_per_port(struct rte_sched_port *port)
284 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport * port->n_subports_per_port;
287 static inline struct rte_mbuf **
288 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
290 uint32_t pindex = qindex >> 4;
291 uint32_t qpos = qindex & 0xF;
293 return (port->queue_array + pindex *
294 port->qsize_sum + port->qsize_add[qpos]);
297 static inline uint16_t
298 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
300 uint32_t tc = (qindex >> 2) & 0x3;
302 return port->qsize[tc];
306 rte_sched_port_check_params(struct rte_sched_port_params *params)
314 if ((params->socket < 0) || (params->socket >= RTE_MAX_NUMA_NODES))
318 if (params->rate == 0)
322 if (params->mtu == 0)
325 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
326 if (params->n_subports_per_port == 0 ||
327 params->n_subports_per_port > 1u << 16 ||
328 !rte_is_power_of_2(params->n_subports_per_port))
331 /* n_pipes_per_subport: non-zero, power of 2 */
332 if (params->n_pipes_per_subport == 0 ||
333 !rte_is_power_of_2(params->n_pipes_per_subport))
336 /* qsize: non-zero, power of 2,
337 * no bigger than 32K (due to 16-bit read/write pointers)
339 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
340 uint16_t qsize = params->qsize[i];
342 if (qsize == 0 || !rte_is_power_of_2(qsize))
346 /* pipe_profiles and n_pipe_profiles */
347 if (params->pipe_profiles == NULL ||
348 params->n_pipe_profiles == 0 ||
349 params->n_pipe_profiles > RTE_SCHED_PIPE_PROFILES_PER_PORT)
352 for (i = 0; i < params->n_pipe_profiles; i++) {
353 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
355 /* TB rate: non-zero, not greater than port rate */
356 if (p->tb_rate == 0 || p->tb_rate > params->rate)
359 /* TB size: non-zero */
363 /* TC rate: non-zero, less than pipe rate */
364 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
365 if (p->tc_rate[j] == 0 || p->tc_rate[j] > p->tb_rate)
369 /* TC period: non-zero */
370 if (p->tc_period == 0)
373 #ifdef RTE_SCHED_SUBPORT_TC_OV
374 /* TC3 oversubscription weight: non-zero */
375 if (p->tc_ov_weight == 0)
379 /* Queue WRR weights: non-zero */
380 for (j = 0; j < RTE_SCHED_QUEUES_PER_PIPE; j++) {
381 if (p->wrr_weights[j] == 0)
390 rte_sched_port_get_array_base(struct rte_sched_port_params *params, enum rte_sched_port_array array)
392 uint32_t n_subports_per_port = params->n_subports_per_port;
393 uint32_t n_pipes_per_subport = params->n_pipes_per_subport;
394 uint32_t n_pipes_per_port = n_pipes_per_subport * n_subports_per_port;
395 uint32_t n_queues_per_port = RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport * n_subports_per_port;
397 uint32_t size_subport = n_subports_per_port * sizeof(struct rte_sched_subport);
398 uint32_t size_pipe = n_pipes_per_port * sizeof(struct rte_sched_pipe);
399 uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue);
400 uint32_t size_queue_extra
401 = n_queues_per_port * sizeof(struct rte_sched_queue_extra);
402 uint32_t size_pipe_profiles
403 = RTE_SCHED_PIPE_PROFILES_PER_PORT * sizeof(struct rte_sched_pipe_profile);
404 uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port);
405 uint32_t size_per_pipe_queue_array, size_queue_array;
409 size_per_pipe_queue_array = 0;
410 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
411 size_per_pipe_queue_array += RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS
412 * params->qsize[i] * sizeof(struct rte_mbuf *);
414 size_queue_array = n_pipes_per_port * size_per_pipe_queue_array;
418 if (array == e_RTE_SCHED_PORT_ARRAY_SUBPORT)
420 base += RTE_CACHE_LINE_ROUNDUP(size_subport);
422 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE)
424 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
426 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE)
428 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
430 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA)
432 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
434 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES)
436 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
438 if (array == e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY)
440 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
442 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY)
444 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
450 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
452 uint32_t size0, size1;
455 status = rte_sched_port_check_params(params);
457 RTE_LOG(NOTICE, SCHED,
458 "Port scheduler params check failed (%d)\n", status);
463 size0 = sizeof(struct rte_sched_port);
464 size1 = rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_TOTAL);
466 return size0 + size1;
470 rte_sched_port_config_qsize(struct rte_sched_port *port)
473 port->qsize_add[0] = 0;
474 port->qsize_add[1] = port->qsize_add[0] + port->qsize[0];
475 port->qsize_add[2] = port->qsize_add[1] + port->qsize[0];
476 port->qsize_add[3] = port->qsize_add[2] + port->qsize[0];
479 port->qsize_add[4] = port->qsize_add[3] + port->qsize[0];
480 port->qsize_add[5] = port->qsize_add[4] + port->qsize[1];
481 port->qsize_add[6] = port->qsize_add[5] + port->qsize[1];
482 port->qsize_add[7] = port->qsize_add[6] + port->qsize[1];
485 port->qsize_add[8] = port->qsize_add[7] + port->qsize[1];
486 port->qsize_add[9] = port->qsize_add[8] + port->qsize[2];
487 port->qsize_add[10] = port->qsize_add[9] + port->qsize[2];
488 port->qsize_add[11] = port->qsize_add[10] + port->qsize[2];
491 port->qsize_add[12] = port->qsize_add[11] + port->qsize[2];
492 port->qsize_add[13] = port->qsize_add[12] + port->qsize[3];
493 port->qsize_add[14] = port->qsize_add[13] + port->qsize[3];
494 port->qsize_add[15] = port->qsize_add[14] + port->qsize[3];
496 port->qsize_sum = port->qsize_add[15] + port->qsize[3];
500 rte_sched_port_log_pipe_profile(struct rte_sched_port *port, uint32_t i)
502 struct rte_sched_pipe_profile *p = port->pipe_profiles + i;
504 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
505 " Token bucket: period = %u, credits per period = %u, size = %u\n"
506 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
507 " Traffic class 3 oversubscription: weight = %hhu\n"
508 " WRR cost: [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu]\n",
513 p->tb_credits_per_period,
516 /* Traffic classes */
518 p->tc_credits_per_period[0],
519 p->tc_credits_per_period[1],
520 p->tc_credits_per_period[2],
521 p->tc_credits_per_period[3],
523 /* Traffic class 3 oversubscription */
527 p->wrr_cost[ 0], p->wrr_cost[ 1], p->wrr_cost[ 2], p->wrr_cost[ 3],
528 p->wrr_cost[ 4], p->wrr_cost[ 5], p->wrr_cost[ 6], p->wrr_cost[ 7],
529 p->wrr_cost[ 8], p->wrr_cost[ 9], p->wrr_cost[10], p->wrr_cost[11],
530 p->wrr_cost[12], p->wrr_cost[13], p->wrr_cost[14], p->wrr_cost[15]);
533 static inline uint64_t
534 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
536 uint64_t time = time_ms;
538 time = (time * rate) / 1000;
544 rte_sched_port_config_pipe_profile_table(struct rte_sched_port *port, struct rte_sched_port_params *params)
548 for (i = 0; i < port->n_pipe_profiles; i++) {
549 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
550 struct rte_sched_pipe_profile *dst = port->pipe_profiles + i;
553 if (src->tb_rate == params->rate) {
554 dst->tb_credits_per_period = 1;
557 double tb_rate = (double) src->tb_rate
558 / (double) params->rate;
559 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
561 rte_approx(tb_rate, d,
562 &dst->tb_credits_per_period, &dst->tb_period);
564 dst->tb_size = src->tb_size;
566 /* Traffic Classes */
567 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
570 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++)
571 dst->tc_credits_per_period[j]
572 = rte_sched_time_ms_to_bytes(src->tc_period,
575 #ifdef RTE_SCHED_SUBPORT_TC_OV
576 dst->tc_ov_weight = src->tc_ov_weight;
580 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
581 uint32_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
582 uint32_t lcd, lcd1, lcd2;
585 qindex = j * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS;
587 wrr_cost[0] = src->wrr_weights[qindex];
588 wrr_cost[1] = src->wrr_weights[qindex + 1];
589 wrr_cost[2] = src->wrr_weights[qindex + 2];
590 wrr_cost[3] = src->wrr_weights[qindex + 3];
592 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
593 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
594 lcd = rte_get_lcd(lcd1, lcd2);
596 wrr_cost[0] = lcd / wrr_cost[0];
597 wrr_cost[1] = lcd / wrr_cost[1];
598 wrr_cost[2] = lcd / wrr_cost[2];
599 wrr_cost[3] = lcd / wrr_cost[3];
601 dst->wrr_cost[qindex] = (uint8_t) wrr_cost[0];
602 dst->wrr_cost[qindex + 1] = (uint8_t) wrr_cost[1];
603 dst->wrr_cost[qindex + 2] = (uint8_t) wrr_cost[2];
604 dst->wrr_cost[qindex + 3] = (uint8_t) wrr_cost[3];
607 rte_sched_port_log_pipe_profile(port, i);
610 port->pipe_tc3_rate_max = 0;
611 for (i = 0; i < port->n_pipe_profiles; i++) {
612 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
613 uint32_t pipe_tc3_rate = src->tc_rate[3];
615 if (port->pipe_tc3_rate_max < pipe_tc3_rate)
616 port->pipe_tc3_rate_max = pipe_tc3_rate;
620 struct rte_sched_port *
621 rte_sched_port_config(struct rte_sched_port_params *params)
623 struct rte_sched_port *port = NULL;
624 uint32_t mem_size, bmp_mem_size, n_queues_per_port, i, cycles_per_byte;
626 /* Check user parameters. Determine the amount of memory to allocate */
627 mem_size = rte_sched_port_get_memory_footprint(params);
631 /* Allocate memory to store the data structures */
632 port = rte_zmalloc("qos_params", mem_size, RTE_CACHE_LINE_SIZE);
636 /* compile time checks */
637 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
638 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS & (RTE_SCHED_PORT_N_GRINDERS - 1));
640 /* User parameters */
641 port->n_subports_per_port = params->n_subports_per_port;
642 port->n_pipes_per_subport = params->n_pipes_per_subport;
643 port->rate = params->rate;
644 port->mtu = params->mtu + params->frame_overhead;
645 port->frame_overhead = params->frame_overhead;
646 memcpy(port->qsize, params->qsize, sizeof(params->qsize));
647 port->n_pipe_profiles = params->n_pipe_profiles;
650 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
653 for (j = 0; j < e_RTE_METER_COLORS; j++) {
654 /* if min/max are both zero, then RED is disabled */
655 if ((params->red_params[i][j].min_th |
656 params->red_params[i][j].max_th) == 0) {
660 if (rte_red_config_init(&port->red_config[i][j],
661 params->red_params[i][j].wq_log2,
662 params->red_params[i][j].min_th,
663 params->red_params[i][j].max_th,
664 params->red_params[i][j].maxp_inv) != 0) {
672 port->time_cpu_cycles = rte_get_tsc_cycles();
673 port->time_cpu_bytes = 0;
676 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
678 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
680 /* Scheduling loop detection */
681 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
682 port->pipe_exhaustion = 0;
685 port->busy_grinders = 0;
686 port->pkts_out = NULL;
687 port->n_pkts_out = 0;
689 /* Queue base calculation */
690 rte_sched_port_config_qsize(port);
692 /* Large data structures */
693 port->subport = (struct rte_sched_subport *)
694 (port->memory + rte_sched_port_get_array_base(params,
695 e_RTE_SCHED_PORT_ARRAY_SUBPORT));
696 port->pipe = (struct rte_sched_pipe *)
697 (port->memory + rte_sched_port_get_array_base(params,
698 e_RTE_SCHED_PORT_ARRAY_PIPE));
699 port->queue = (struct rte_sched_queue *)
700 (port->memory + rte_sched_port_get_array_base(params,
701 e_RTE_SCHED_PORT_ARRAY_QUEUE));
702 port->queue_extra = (struct rte_sched_queue_extra *)
703 (port->memory + rte_sched_port_get_array_base(params,
704 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA));
705 port->pipe_profiles = (struct rte_sched_pipe_profile *)
706 (port->memory + rte_sched_port_get_array_base(params,
707 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES));
708 port->bmp_array = port->memory
709 + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY);
710 port->queue_array = (struct rte_mbuf **)
711 (port->memory + rte_sched_port_get_array_base(params,
712 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY));
714 /* Pipe profile table */
715 rte_sched_port_config_pipe_profile_table(port, params);
718 n_queues_per_port = rte_sched_port_queues_per_port(port);
719 bmp_mem_size = rte_bitmap_get_memory_footprint(n_queues_per_port);
720 port->bmp = rte_bitmap_init(n_queues_per_port, port->bmp_array,
722 if (port->bmp == NULL) {
723 RTE_LOG(ERR, SCHED, "Bitmap init error\n");
727 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
728 port->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
735 rte_sched_port_free(struct rte_sched_port *port)
738 uint32_t n_queues_per_port;
740 /* Check user parameters */
744 n_queues_per_port = rte_sched_port_queues_per_port(port);
746 /* Free enqueued mbufs */
747 for (qindex = 0; qindex < n_queues_per_port; qindex++) {
748 struct rte_mbuf **mbufs = rte_sched_port_qbase(port, qindex);
749 uint16_t qsize = rte_sched_port_qsize(port, qindex);
750 struct rte_sched_queue *queue = port->queue + qindex;
751 uint16_t qr = queue->qr & (qsize - 1);
752 uint16_t qw = queue->qw & (qsize - 1);
754 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
755 rte_pktmbuf_free(mbufs[qr]);
758 rte_bitmap_free(port->bmp);
763 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
765 struct rte_sched_subport *s = port->subport + i;
767 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
768 " Token bucket: period = %u, credits per period = %u, size = %u\n"
769 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
770 " Traffic class 3 oversubscription: wm min = %u, wm max = %u\n",
775 s->tb_credits_per_period,
778 /* Traffic classes */
780 s->tc_credits_per_period[0],
781 s->tc_credits_per_period[1],
782 s->tc_credits_per_period[2],
783 s->tc_credits_per_period[3],
785 /* Traffic class 3 oversubscription */
791 rte_sched_subport_config(struct rte_sched_port *port,
793 struct rte_sched_subport_params *params)
795 struct rte_sched_subport *s;
798 /* Check user parameters */
800 subport_id >= port->n_subports_per_port ||
804 if (params->tb_rate == 0 || params->tb_rate > port->rate)
807 if (params->tb_size == 0)
810 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
811 if (params->tc_rate[i] == 0 ||
812 params->tc_rate[i] > params->tb_rate)
816 if (params->tc_period == 0)
819 s = port->subport + subport_id;
821 /* Token Bucket (TB) */
822 if (params->tb_rate == port->rate) {
823 s->tb_credits_per_period = 1;
826 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
827 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
829 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
832 s->tb_size = params->tb_size;
833 s->tb_time = port->time;
834 s->tb_credits = s->tb_size / 2;
836 /* Traffic Classes (TCs) */
837 s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
838 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
839 s->tc_credits_per_period[i]
840 = rte_sched_time_ms_to_bytes(params->tc_period,
843 s->tc_time = port->time + s->tc_period;
844 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
845 s->tc_credits[i] = s->tc_credits_per_period[i];
847 #ifdef RTE_SCHED_SUBPORT_TC_OV
848 /* TC oversubscription */
849 s->tc_ov_wm_min = port->mtu;
850 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
851 port->pipe_tc3_rate_max);
852 s->tc_ov_wm = s->tc_ov_wm_max;
853 s->tc_ov_period_id = 0;
859 rte_sched_port_log_subport_config(port, subport_id);
865 rte_sched_pipe_config(struct rte_sched_port *port,
868 int32_t pipe_profile)
870 struct rte_sched_subport *s;
871 struct rte_sched_pipe *p;
872 struct rte_sched_pipe_profile *params;
873 uint32_t deactivate, profile, i;
875 /* Check user parameters */
876 profile = (uint32_t) pipe_profile;
877 deactivate = (pipe_profile < 0);
880 subport_id >= port->n_subports_per_port ||
881 pipe_id >= port->n_pipes_per_subport ||
882 (!deactivate && profile >= port->n_pipe_profiles))
886 /* Check that subport configuration is valid */
887 s = port->subport + subport_id;
888 if (s->tb_period == 0)
891 p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
893 /* Handle the case when pipe already has a valid configuration */
895 params = port->pipe_profiles + p->profile;
897 #ifdef RTE_SCHED_SUBPORT_TC_OV
898 double subport_tc3_rate = (double) s->tc_credits_per_period[3]
899 / (double) s->tc_period;
900 double pipe_tc3_rate = (double) params->tc_credits_per_period[3]
901 / (double) params->tc_period;
902 uint32_t tc3_ov = s->tc_ov;
904 /* Unplug pipe from its subport */
905 s->tc_ov_n -= params->tc_ov_weight;
906 s->tc_ov_rate -= pipe_tc3_rate;
907 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
909 if (s->tc_ov != tc3_ov) {
910 RTE_LOG(DEBUG, SCHED,
911 "Subport %u TC3 oversubscription is OFF (%.4lf >= %.4lf)\n",
912 subport_id, subport_tc3_rate, s->tc_ov_rate);
917 memset(p, 0, sizeof(struct rte_sched_pipe));
923 /* Apply the new pipe configuration */
924 p->profile = profile;
925 params = port->pipe_profiles + p->profile;
927 /* Token Bucket (TB) */
928 p->tb_time = port->time;
929 p->tb_credits = params->tb_size / 2;
931 /* Traffic Classes (TCs) */
932 p->tc_time = port->time + params->tc_period;
933 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
934 p->tc_credits[i] = params->tc_credits_per_period[i];
936 #ifdef RTE_SCHED_SUBPORT_TC_OV
938 /* Subport TC3 oversubscription */
939 double subport_tc3_rate = (double) s->tc_credits_per_period[3]
940 / (double) s->tc_period;
941 double pipe_tc3_rate = (double) params->tc_credits_per_period[3]
942 / (double) params->tc_period;
943 uint32_t tc3_ov = s->tc_ov;
945 s->tc_ov_n += params->tc_ov_weight;
946 s->tc_ov_rate += pipe_tc3_rate;
947 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
949 if (s->tc_ov != tc3_ov) {
950 RTE_LOG(DEBUG, SCHED,
951 "Subport %u TC3 oversubscription is ON (%.4lf < %.4lf)\n",
952 subport_id, subport_tc3_rate, s->tc_ov_rate);
954 p->tc_ov_period_id = s->tc_ov_period_id;
955 p->tc_ov_credits = s->tc_ov_wm;
963 rte_sched_port_pkt_write(struct rte_mbuf *pkt,
964 uint32_t subport, uint32_t pipe, uint32_t traffic_class,
965 uint32_t queue, enum rte_meter_color color)
967 struct rte_sched_port_hierarchy *sched
968 = (struct rte_sched_port_hierarchy *) &pkt->hash.sched;
970 RTE_BUILD_BUG_ON(sizeof(*sched) > sizeof(pkt->hash.sched));
972 sched->color = (uint32_t) color;
973 sched->subport = subport;
975 sched->traffic_class = traffic_class;
976 sched->queue = queue;
980 rte_sched_port_pkt_read_tree_path(const struct rte_mbuf *pkt,
981 uint32_t *subport, uint32_t *pipe,
982 uint32_t *traffic_class, uint32_t *queue)
984 const struct rte_sched_port_hierarchy *sched
985 = (const struct rte_sched_port_hierarchy *) &pkt->hash.sched;
987 *subport = sched->subport;
989 *traffic_class = sched->traffic_class;
990 *queue = sched->queue;
994 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
996 const struct rte_sched_port_hierarchy *sched
997 = (const struct rte_sched_port_hierarchy *) &pkt->hash.sched;
999 return (enum rte_meter_color) sched->color;
1003 rte_sched_subport_read_stats(struct rte_sched_port *port,
1004 uint32_t subport_id,
1005 struct rte_sched_subport_stats *stats,
1008 struct rte_sched_subport *s;
1010 /* Check user parameters */
1011 if (port == NULL || subport_id >= port->n_subports_per_port ||
1012 stats == NULL || tc_ov == NULL)
1015 s = port->subport + subport_id;
1017 /* Copy subport stats and clear */
1018 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1019 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1021 /* Subport TC ovesubscription status */
1028 rte_sched_queue_read_stats(struct rte_sched_port *port,
1030 struct rte_sched_queue_stats *stats,
1033 struct rte_sched_queue *q;
1034 struct rte_sched_queue_extra *qe;
1036 /* Check user parameters */
1037 if ((port == NULL) ||
1038 (queue_id >= rte_sched_port_queues_per_port(port)) ||
1043 q = port->queue + queue_id;
1044 qe = port->queue_extra + queue_id;
1046 /* Copy queue stats and clear */
1047 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1048 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1051 *qlen = q->qw - q->qr;
1056 static inline uint32_t
1057 rte_sched_port_qindex(struct rte_sched_port *port, uint32_t subport, uint32_t pipe, uint32_t traffic_class, uint32_t queue)
1061 result = subport * port->n_pipes_per_subport + pipe;
1062 result = result * RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE + traffic_class;
1063 result = result * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
1068 #ifdef RTE_SCHED_DEBUG
1071 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1073 struct rte_sched_queue *queue = port->queue + qindex;
1075 return queue->qr == queue->qw;
1078 #endif /* RTE_SCHED_DEBUG */
1080 #ifdef RTE_SCHED_COLLECT_STATS
1083 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1085 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1086 uint32_t tc_index = (qindex >> 2) & 0x3;
1087 uint32_t pkt_len = pkt->pkt_len;
1089 s->stats.n_pkts_tc[tc_index] += 1;
1090 s->stats.n_bytes_tc[tc_index] += pkt_len;
1093 #ifdef RTE_SCHED_RED
1095 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1097 struct rte_mbuf *pkt, uint32_t red)
1100 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1102 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1105 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1106 uint32_t tc_index = (qindex >> 2) & 0x3;
1107 uint32_t pkt_len = pkt->pkt_len;
1109 s->stats.n_pkts_tc_dropped[tc_index] += 1;
1110 s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1111 #ifdef RTE_SCHED_RED
1112 s->stats.n_pkts_red_dropped[tc_index] += red;
1117 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1119 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1120 uint32_t pkt_len = pkt->pkt_len;
1122 qe->stats.n_pkts += 1;
1123 qe->stats.n_bytes += pkt_len;
1126 #ifdef RTE_SCHED_RED
1128 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1130 struct rte_mbuf *pkt, uint32_t red)
1133 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1135 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1138 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1139 uint32_t pkt_len = pkt->pkt_len;
1141 qe->stats.n_pkts_dropped += 1;
1142 qe->stats.n_bytes_dropped += pkt_len;
1143 #ifdef RTE_SCHED_RED
1144 qe->stats.n_pkts_red_dropped += red;
1148 #endif /* RTE_SCHED_COLLECT_STATS */
1150 #ifdef RTE_SCHED_RED
1153 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1155 struct rte_sched_queue_extra *qe;
1156 struct rte_red_config *red_cfg;
1157 struct rte_red *red;
1159 enum rte_meter_color color;
1161 tc_index = (qindex >> 2) & 0x3;
1162 color = rte_sched_port_pkt_read_color(pkt);
1163 red_cfg = &port->red_config[tc_index][color];
1165 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1168 qe = port->queue_extra + qindex;
1171 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1175 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1177 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1178 struct rte_red *red = &qe->red;
1180 rte_red_mark_queue_empty(red, port->time);
1185 #define rte_sched_port_red_drop(port, pkt, qindex, qlen) 0
1187 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1189 #endif /* RTE_SCHED_RED */
1191 #ifdef RTE_SCHED_DEBUG
1194 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1201 rte_panic("Empty slab at position %u\n", bmp_pos);
1204 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1205 if (mask & bmp_slab) {
1206 if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1207 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1214 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1218 #endif /* RTE_SCHED_DEBUG */
1220 static inline uint32_t
1221 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port,
1222 struct rte_mbuf *pkt)
1224 struct rte_sched_queue *q;
1225 #ifdef RTE_SCHED_COLLECT_STATS
1226 struct rte_sched_queue_extra *qe;
1228 uint32_t subport, pipe, traffic_class, queue, qindex;
1230 rte_sched_port_pkt_read_tree_path(pkt, &subport, &pipe, &traffic_class, &queue);
1232 qindex = rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1233 q = port->queue + qindex;
1235 #ifdef RTE_SCHED_COLLECT_STATS
1236 qe = port->queue_extra + qindex;
1244 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1245 uint32_t qindex, struct rte_mbuf **qbase)
1247 struct rte_sched_queue *q;
1248 struct rte_mbuf **q_qw;
1251 q = port->queue + qindex;
1252 qsize = rte_sched_port_qsize(port, qindex);
1253 q_qw = qbase + (q->qw & (qsize - 1));
1255 rte_prefetch0(q_qw);
1256 rte_bitmap_prefetch0(port->bmp, qindex);
1260 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex,
1261 struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1263 struct rte_sched_queue *q;
1267 q = port->queue + qindex;
1268 qsize = rte_sched_port_qsize(port, qindex);
1269 qlen = q->qw - q->qr;
1271 /* Drop the packet (and update drop stats) when queue is full */
1272 if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) ||
1274 rte_pktmbuf_free(pkt);
1275 #ifdef RTE_SCHED_COLLECT_STATS
1276 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt,
1278 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt,
1284 /* Enqueue packet */
1285 qbase[q->qw & (qsize - 1)] = pkt;
1288 /* Activate queue in the port bitmap */
1289 rte_bitmap_set(port->bmp, qindex);
1292 #ifdef RTE_SCHED_COLLECT_STATS
1293 rte_sched_port_update_subport_stats(port, qindex, pkt);
1294 rte_sched_port_update_queue_stats(port, qindex, pkt);
1302 * The enqueue function implements a 4-level pipeline with each stage
1303 * processing two different packets. The purpose of using a pipeline
1304 * is to hide the latency of prefetching the data structures. The
1305 * naming convention is presented in the diagram below:
1307 * p00 _______ p10 _______ p20 _______ p30 _______
1308 * ----->| |----->| |----->| |----->| |----->
1309 * | 0 | | 1 | | 2 | | 3 |
1310 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1315 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1318 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1319 *pkt30, *pkt31, *pkt_last;
1320 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1321 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1322 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1323 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1329 * Less then 6 input packets available, which is not enough to
1332 if (unlikely(n_pkts < 6)) {
1333 struct rte_mbuf **q_base[5];
1336 /* Prefetch the mbuf structure of each packet */
1337 for (i = 0; i < n_pkts; i++)
1338 rte_prefetch0(pkts[i]);
1340 /* Prefetch the queue structure for each queue */
1341 for (i = 0; i < n_pkts; i++)
1342 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port,
1345 /* Prefetch the write pointer location of each queue */
1346 for (i = 0; i < n_pkts; i++) {
1347 q_base[i] = rte_sched_port_qbase(port, q[i]);
1348 rte_sched_port_enqueue_qwa_prefetch0(port, q[i],
1352 /* Write each packet to its queue */
1353 for (i = 0; i < n_pkts; i++)
1354 result += rte_sched_port_enqueue_qwa(port, q[i],
1355 q_base[i], pkts[i]);
1360 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1363 rte_prefetch0(pkt20);
1364 rte_prefetch0(pkt21);
1368 rte_prefetch0(pkt10);
1369 rte_prefetch0(pkt11);
1371 q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1372 q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1376 rte_prefetch0(pkt00);
1377 rte_prefetch0(pkt01);
1379 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1380 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1382 q20_base = rte_sched_port_qbase(port, q20);
1383 q21_base = rte_sched_port_qbase(port, q21);
1384 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1385 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1387 /* Run the pipeline */
1388 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1389 /* Propagate stage inputs */
1400 q30_base = q20_base;
1401 q31_base = q21_base;
1403 /* Stage 0: Get packets in */
1405 pkt01 = pkts[i + 1];
1406 rte_prefetch0(pkt00);
1407 rte_prefetch0(pkt01);
1409 /* Stage 1: Prefetch queue structure storing queue pointers */
1410 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1411 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1413 /* Stage 2: Prefetch queue write location */
1414 q20_base = rte_sched_port_qbase(port, q20);
1415 q21_base = rte_sched_port_qbase(port, q21);
1416 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1417 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1419 /* Stage 3: Write packet to queue and activate queue */
1420 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1421 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1422 result += r30 + r31;
1426 * Drain the pipeline (exactly 6 packets).
1427 * Handle the last packet in the case
1428 * of an odd number of input packets.
1430 pkt_last = pkts[n_pkts - 1];
1431 rte_prefetch0(pkt_last);
1433 q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1434 q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1436 q10_base = rte_sched_port_qbase(port, q10);
1437 q11_base = rte_sched_port_qbase(port, q11);
1438 rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1439 rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1441 r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1442 r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1443 result += r20 + r21;
1445 q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1447 q00_base = rte_sched_port_qbase(port, q00);
1448 q01_base = rte_sched_port_qbase(port, q01);
1449 rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1450 rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1452 r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1453 r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1454 result += r10 + r11;
1456 q_last_base = rte_sched_port_qbase(port, q_last);
1457 rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1459 r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1460 r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1461 result += r00 + r01;
1464 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1471 #ifndef RTE_SCHED_SUBPORT_TC_OV
1474 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1476 struct rte_sched_grinder *grinder = port->grinder + pos;
1477 struct rte_sched_subport *subport = grinder->subport;
1478 struct rte_sched_pipe *pipe = grinder->pipe;
1479 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1483 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1484 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1485 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1486 subport->tb_time += n_periods * subport->tb_period;
1489 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1490 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1491 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1492 pipe->tb_time += n_periods * params->tb_period;
1495 if (unlikely(port->time >= subport->tc_time)) {
1496 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1497 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1498 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1499 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1500 subport->tc_time = port->time + subport->tc_period;
1504 if (unlikely(port->time >= pipe->tc_time)) {
1505 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1506 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1507 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1508 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1509 pipe->tc_time = port->time + params->tc_period;
1515 static inline uint32_t
1516 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1518 struct rte_sched_grinder *grinder = port->grinder + pos;
1519 struct rte_sched_subport *subport = grinder->subport;
1520 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1521 uint32_t tc_ov_consumption_max;
1522 uint32_t tc_ov_wm = subport->tc_ov_wm;
1524 if (subport->tc_ov == 0)
1525 return subport->tc_ov_wm_max;
1527 tc_ov_consumption[0] = subport->tc_credits_per_period[0] - subport->tc_credits[0];
1528 tc_ov_consumption[1] = subport->tc_credits_per_period[1] - subport->tc_credits[1];
1529 tc_ov_consumption[2] = subport->tc_credits_per_period[2] - subport->tc_credits[2];
1530 tc_ov_consumption[3] = subport->tc_credits_per_period[3] - subport->tc_credits[3];
1532 tc_ov_consumption_max = subport->tc_credits_per_period[3] -
1533 (tc_ov_consumption[0] + tc_ov_consumption[1] + tc_ov_consumption[2]);
1535 if (tc_ov_consumption[3] > (tc_ov_consumption_max - port->mtu)) {
1536 tc_ov_wm -= tc_ov_wm >> 7;
1537 if (tc_ov_wm < subport->tc_ov_wm_min)
1538 tc_ov_wm = subport->tc_ov_wm_min;
1543 tc_ov_wm += (tc_ov_wm >> 7) + 1;
1544 if (tc_ov_wm > subport->tc_ov_wm_max)
1545 tc_ov_wm = subport->tc_ov_wm_max;
1551 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1553 struct rte_sched_grinder *grinder = port->grinder + pos;
1554 struct rte_sched_subport *subport = grinder->subport;
1555 struct rte_sched_pipe *pipe = grinder->pipe;
1556 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1560 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1561 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1562 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1563 subport->tb_time += n_periods * subport->tb_period;
1566 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1567 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1568 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1569 pipe->tb_time += n_periods * params->tb_period;
1572 if (unlikely(port->time >= subport->tc_time)) {
1573 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
1575 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1576 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1577 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1578 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1580 subport->tc_time = port->time + subport->tc_period;
1581 subport->tc_ov_period_id++;
1585 if (unlikely(port->time >= pipe->tc_time)) {
1586 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1587 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1588 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1589 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1590 pipe->tc_time = port->time + params->tc_period;
1593 /* Pipe TCs - Oversubscription */
1594 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
1595 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
1597 pipe->tc_ov_period_id = subport->tc_ov_period_id;
1601 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
1604 #ifndef RTE_SCHED_SUBPORT_TC_OV
1607 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1609 struct rte_sched_grinder *grinder = port->grinder + pos;
1610 struct rte_sched_subport *subport = grinder->subport;
1611 struct rte_sched_pipe *pipe = grinder->pipe;
1612 struct rte_mbuf *pkt = grinder->pkt;
1613 uint32_t tc_index = grinder->tc_index;
1614 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1615 uint32_t subport_tb_credits = subport->tb_credits;
1616 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1617 uint32_t pipe_tb_credits = pipe->tb_credits;
1618 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1621 /* Check queue credits */
1622 enough_credits = (pkt_len <= subport_tb_credits) &&
1623 (pkt_len <= subport_tc_credits) &&
1624 (pkt_len <= pipe_tb_credits) &&
1625 (pkt_len <= pipe_tc_credits);
1627 if (!enough_credits)
1630 /* Update port credits */
1631 subport->tb_credits -= pkt_len;
1632 subport->tc_credits[tc_index] -= pkt_len;
1633 pipe->tb_credits -= pkt_len;
1634 pipe->tc_credits[tc_index] -= pkt_len;
1642 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1644 struct rte_sched_grinder *grinder = port->grinder + pos;
1645 struct rte_sched_subport *subport = grinder->subport;
1646 struct rte_sched_pipe *pipe = grinder->pipe;
1647 struct rte_mbuf *pkt = grinder->pkt;
1648 uint32_t tc_index = grinder->tc_index;
1649 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1650 uint32_t subport_tb_credits = subport->tb_credits;
1651 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1652 uint32_t pipe_tb_credits = pipe->tb_credits;
1653 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1654 uint32_t pipe_tc_ov_mask1[] = {UINT32_MAX, UINT32_MAX, UINT32_MAX, pipe->tc_ov_credits};
1655 uint32_t pipe_tc_ov_mask2[] = {0, 0, 0, UINT32_MAX};
1656 uint32_t pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
1659 /* Check pipe and subport credits */
1660 enough_credits = (pkt_len <= subport_tb_credits) &&
1661 (pkt_len <= subport_tc_credits) &&
1662 (pkt_len <= pipe_tb_credits) &&
1663 (pkt_len <= pipe_tc_credits) &&
1664 (pkt_len <= pipe_tc_ov_credits);
1666 if (!enough_credits)
1669 /* Update pipe and subport credits */
1670 subport->tb_credits -= pkt_len;
1671 subport->tc_credits[tc_index] -= pkt_len;
1672 pipe->tb_credits -= pkt_len;
1673 pipe->tc_credits[tc_index] -= pkt_len;
1674 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
1679 #endif /* RTE_SCHED_SUBPORT_TC_OV */
1683 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
1685 struct rte_sched_grinder *grinder = port->grinder + pos;
1686 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
1687 struct rte_mbuf *pkt = grinder->pkt;
1688 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1690 if (!grinder_credits_check(port, pos))
1693 /* Advance port time */
1694 port->time += pkt_len;
1697 port->pkts_out[port->n_pkts_out++] = pkt;
1699 grinder->wrr_tokens[grinder->qpos] += pkt_len * grinder->wrr_cost[grinder->qpos];
1700 if (queue->qr == queue->qw) {
1701 uint32_t qindex = grinder->qindex[grinder->qpos];
1703 rte_bitmap_clear(port->bmp, qindex);
1704 grinder->qmask &= ~(1 << grinder->qpos);
1705 grinder->wrr_mask[grinder->qpos] = 0;
1706 rte_sched_port_set_queue_empty_timestamp(port, qindex);
1709 /* Reset pipe loop detection */
1710 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1711 grinder->productive = 1;
1716 #ifdef SCHED_VECTOR_SSE4
1719 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1721 __m128i index = _mm_set1_epi32(base_pipe);
1722 __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
1723 __m128i res = _mm_cmpeq_epi32(pipes, index);
1725 pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
1726 pipes = _mm_cmpeq_epi32(pipes, index);
1727 res = _mm_or_si128(res, pipes);
1729 if (_mm_testz_si128(res, res))
1738 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1742 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
1743 if (port->grinder_base_bmp_pos[i] == base_pipe)
1750 #endif /* RTE_SCHED_OPTIMIZATIONS */
1753 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
1755 struct rte_sched_grinder *grinder = port->grinder + pos;
1758 grinder->pcache_w = 0;
1759 grinder->pcache_r = 0;
1761 w[0] = (uint16_t) bmp_slab;
1762 w[1] = (uint16_t) (bmp_slab >> 16);
1763 w[2] = (uint16_t) (bmp_slab >> 32);
1764 w[3] = (uint16_t) (bmp_slab >> 48);
1766 grinder->pcache_qmask[grinder->pcache_w] = w[0];
1767 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
1768 grinder->pcache_w += (w[0] != 0);
1770 grinder->pcache_qmask[grinder->pcache_w] = w[1];
1771 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
1772 grinder->pcache_w += (w[1] != 0);
1774 grinder->pcache_qmask[grinder->pcache_w] = w[2];
1775 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
1776 grinder->pcache_w += (w[2] != 0);
1778 grinder->pcache_qmask[grinder->pcache_w] = w[3];
1779 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
1780 grinder->pcache_w += (w[3] != 0);
1784 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
1786 struct rte_sched_grinder *grinder = port->grinder + pos;
1789 grinder->tccache_w = 0;
1790 grinder->tccache_r = 0;
1792 b[0] = (uint8_t) (qmask & 0xF);
1793 b[1] = (uint8_t) ((qmask >> 4) & 0xF);
1794 b[2] = (uint8_t) ((qmask >> 8) & 0xF);
1795 b[3] = (uint8_t) ((qmask >> 12) & 0xF);
1797 grinder->tccache_qmask[grinder->tccache_w] = b[0];
1798 grinder->tccache_qindex[grinder->tccache_w] = qindex;
1799 grinder->tccache_w += (b[0] != 0);
1801 grinder->tccache_qmask[grinder->tccache_w] = b[1];
1802 grinder->tccache_qindex[grinder->tccache_w] = qindex + 4;
1803 grinder->tccache_w += (b[1] != 0);
1805 grinder->tccache_qmask[grinder->tccache_w] = b[2];
1806 grinder->tccache_qindex[grinder->tccache_w] = qindex + 8;
1807 grinder->tccache_w += (b[2] != 0);
1809 grinder->tccache_qmask[grinder->tccache_w] = b[3];
1810 grinder->tccache_qindex[grinder->tccache_w] = qindex + 12;
1811 grinder->tccache_w += (b[3] != 0);
1815 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
1817 struct rte_sched_grinder *grinder = port->grinder + pos;
1818 struct rte_mbuf **qbase;
1822 if (grinder->tccache_r == grinder->tccache_w)
1825 qindex = grinder->tccache_qindex[grinder->tccache_r];
1826 qbase = rte_sched_port_qbase(port, qindex);
1827 qsize = rte_sched_port_qsize(port, qindex);
1829 grinder->tc_index = (qindex >> 2) & 0x3;
1830 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
1831 grinder->qsize = qsize;
1833 grinder->qindex[0] = qindex;
1834 grinder->qindex[1] = qindex + 1;
1835 grinder->qindex[2] = qindex + 2;
1836 grinder->qindex[3] = qindex + 3;
1838 grinder->queue[0] = port->queue + qindex;
1839 grinder->queue[1] = port->queue + qindex + 1;
1840 grinder->queue[2] = port->queue + qindex + 2;
1841 grinder->queue[3] = port->queue + qindex + 3;
1843 grinder->qbase[0] = qbase;
1844 grinder->qbase[1] = qbase + qsize;
1845 grinder->qbase[2] = qbase + 2 * qsize;
1846 grinder->qbase[3] = qbase + 3 * qsize;
1848 grinder->tccache_r++;
1853 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
1855 struct rte_sched_grinder *grinder = port->grinder + pos;
1856 uint32_t pipe_qindex;
1857 uint16_t pipe_qmask;
1859 if (grinder->pcache_r < grinder->pcache_w) {
1860 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
1861 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
1862 grinder->pcache_r++;
1864 uint64_t bmp_slab = 0;
1865 uint32_t bmp_pos = 0;
1867 /* Get another non-empty pipe group */
1868 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
1871 #ifdef RTE_SCHED_DEBUG
1872 debug_check_queue_slab(port, bmp_pos, bmp_slab);
1875 /* Return if pipe group already in one of the other grinders */
1876 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
1877 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
1880 port->grinder_base_bmp_pos[pos] = bmp_pos;
1882 /* Install new pipe group into grinder's pipe cache */
1883 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
1885 pipe_qmask = grinder->pcache_qmask[0];
1886 pipe_qindex = grinder->pcache_qindex[0];
1887 grinder->pcache_r = 1;
1890 /* Install new pipe in the grinder */
1891 grinder->pindex = pipe_qindex >> 4;
1892 grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
1893 grinder->pipe = port->pipe + grinder->pindex;
1894 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
1895 grinder->productive = 0;
1897 grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
1898 grinder_next_tc(port, pos);
1900 /* Check for pipe exhaustion */
1901 if (grinder->pindex == port->pipe_loop) {
1902 port->pipe_exhaustion = 1;
1903 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1911 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
1913 struct rte_sched_grinder *grinder = port->grinder + pos;
1914 struct rte_sched_pipe *pipe = grinder->pipe;
1915 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
1916 uint32_t tc_index = grinder->tc_index;
1917 uint32_t qmask = grinder->qmask;
1920 qindex = tc_index * 4;
1922 grinder->wrr_tokens[0] = ((uint16_t) pipe->wrr_tokens[qindex]) << RTE_SCHED_WRR_SHIFT;
1923 grinder->wrr_tokens[1] = ((uint16_t) pipe->wrr_tokens[qindex + 1]) << RTE_SCHED_WRR_SHIFT;
1924 grinder->wrr_tokens[2] = ((uint16_t) pipe->wrr_tokens[qindex + 2]) << RTE_SCHED_WRR_SHIFT;
1925 grinder->wrr_tokens[3] = ((uint16_t) pipe->wrr_tokens[qindex + 3]) << RTE_SCHED_WRR_SHIFT;
1927 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
1928 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
1929 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
1930 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
1932 grinder->wrr_cost[0] = pipe_params->wrr_cost[qindex];
1933 grinder->wrr_cost[1] = pipe_params->wrr_cost[qindex + 1];
1934 grinder->wrr_cost[2] = pipe_params->wrr_cost[qindex + 2];
1935 grinder->wrr_cost[3] = pipe_params->wrr_cost[qindex + 3];
1939 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
1941 struct rte_sched_grinder *grinder = port->grinder + pos;
1942 struct rte_sched_pipe *pipe = grinder->pipe;
1943 uint32_t tc_index = grinder->tc_index;
1946 qindex = tc_index * 4;
1948 pipe->wrr_tokens[qindex] = (grinder->wrr_tokens[0] & grinder->wrr_mask[0])
1949 >> RTE_SCHED_WRR_SHIFT;
1950 pipe->wrr_tokens[qindex + 1] = (grinder->wrr_tokens[1] & grinder->wrr_mask[1])
1951 >> RTE_SCHED_WRR_SHIFT;
1952 pipe->wrr_tokens[qindex + 2] = (grinder->wrr_tokens[2] & grinder->wrr_mask[2])
1953 >> RTE_SCHED_WRR_SHIFT;
1954 pipe->wrr_tokens[qindex + 3] = (grinder->wrr_tokens[3] & grinder->wrr_mask[3])
1955 >> RTE_SCHED_WRR_SHIFT;
1959 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
1961 struct rte_sched_grinder *grinder = port->grinder + pos;
1962 uint16_t wrr_tokens_min;
1964 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
1965 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
1966 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
1967 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
1969 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
1970 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
1972 grinder->wrr_tokens[0] -= wrr_tokens_min;
1973 grinder->wrr_tokens[1] -= wrr_tokens_min;
1974 grinder->wrr_tokens[2] -= wrr_tokens_min;
1975 grinder->wrr_tokens[3] -= wrr_tokens_min;
1979 #define grinder_evict(port, pos)
1982 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
1984 struct rte_sched_grinder *grinder = port->grinder + pos;
1986 rte_prefetch0(grinder->pipe);
1987 rte_prefetch0(grinder->queue[0]);
1991 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
1993 struct rte_sched_grinder *grinder = port->grinder + pos;
1994 uint16_t qsize, qr[4];
1996 qsize = grinder->qsize;
1997 qr[0] = grinder->queue[0]->qr & (qsize - 1);
1998 qr[1] = grinder->queue[1]->qr & (qsize - 1);
1999 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2000 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2002 rte_prefetch0(grinder->qbase[0] + qr[0]);
2003 rte_prefetch0(grinder->qbase[1] + qr[1]);
2005 grinder_wrr_load(port, pos);
2006 grinder_wrr(port, pos);
2008 rte_prefetch0(grinder->qbase[2] + qr[2]);
2009 rte_prefetch0(grinder->qbase[3] + qr[3]);
2013 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2015 struct rte_sched_grinder *grinder = port->grinder + pos;
2016 uint32_t qpos = grinder->qpos;
2017 struct rte_mbuf **qbase = grinder->qbase[qpos];
2018 uint16_t qsize = grinder->qsize;
2019 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2021 grinder->pkt = qbase[qr];
2022 rte_prefetch0(grinder->pkt);
2024 if (unlikely((qr & 0x7) == 7)) {
2025 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2027 rte_prefetch0(qbase + qr_next);
2031 static inline uint32_t
2032 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2034 struct rte_sched_grinder *grinder = port->grinder + pos;
2036 switch (grinder->state) {
2037 case e_GRINDER_PREFETCH_PIPE:
2039 if (grinder_next_pipe(port, pos)) {
2040 grinder_prefetch_pipe(port, pos);
2041 port->busy_grinders++;
2043 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2050 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2052 struct rte_sched_pipe *pipe = grinder->pipe;
2054 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2055 grinder_prefetch_tc_queue_arrays(port, pos);
2056 grinder_credits_update(port, pos);
2058 grinder->state = e_GRINDER_PREFETCH_MBUF;
2062 case e_GRINDER_PREFETCH_MBUF:
2064 grinder_prefetch_mbuf(port, pos);
2066 grinder->state = e_GRINDER_READ_MBUF;
2070 case e_GRINDER_READ_MBUF:
2072 uint32_t result = 0;
2074 result = grinder_schedule(port, pos);
2076 /* Look for next packet within the same TC */
2077 if (result && grinder->qmask) {
2078 grinder_wrr(port, pos);
2079 grinder_prefetch_mbuf(port, pos);
2083 grinder_wrr_store(port, pos);
2085 /* Look for another active TC within same pipe */
2086 if (grinder_next_tc(port, pos)) {
2087 grinder_prefetch_tc_queue_arrays(port, pos);
2089 grinder->state = e_GRINDER_PREFETCH_MBUF;
2093 if (grinder->productive == 0 &&
2094 port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2095 port->pipe_loop = grinder->pindex;
2097 grinder_evict(port, pos);
2099 /* Look for another active pipe */
2100 if (grinder_next_pipe(port, pos)) {
2101 grinder_prefetch_pipe(port, pos);
2103 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2107 /* No active pipe found */
2108 port->busy_grinders--;
2110 grinder->state = e_GRINDER_PREFETCH_PIPE;
2115 rte_panic("Algorithmic error (invalid state)\n");
2121 rte_sched_port_time_resync(struct rte_sched_port *port)
2123 uint64_t cycles = rte_get_tsc_cycles();
2124 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2125 uint64_t bytes_diff;
2127 /* Compute elapsed time in bytes */
2128 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2129 port->inv_cycles_per_byte);
2131 /* Advance port time */
2132 port->time_cpu_cycles = cycles;
2133 port->time_cpu_bytes += bytes_diff;
2134 if (port->time < port->time_cpu_bytes)
2135 port->time = port->time_cpu_bytes;
2137 /* Reset pipe loop detection */
2138 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2142 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2146 /* Check if any exception flag is set */
2147 exceptions = (second_pass && port->busy_grinders == 0) ||
2148 (port->pipe_exhaustion == 1);
2150 /* Clear exception flags */
2151 port->pipe_exhaustion = 0;
2157 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2161 port->pkts_out = pkts;
2162 port->n_pkts_out = 0;
2164 rte_sched_port_time_resync(port);
2166 /* Take each queue in the grinder one step further */
2167 for (i = 0, count = 0; ; i++) {
2168 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2169 if ((count == n_pkts) ||
2170 rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {