1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_common.h>
10 #include <rte_memory.h>
11 #include <rte_malloc.h>
12 #include <rte_cycles.h>
13 #include <rte_prefetch.h>
14 #include <rte_branch_prediction.h>
16 #include <rte_bitmap.h>
17 #include <rte_reciprocal.h>
19 #include "rte_sched.h"
20 #include "rte_sched_common.h"
21 #include "rte_approx.h"
23 #ifdef __INTEL_COMPILER
24 #pragma warning(disable:2259) /* conversion may lose significant bits */
27 #ifdef RTE_SCHED_VECTOR
31 #define SCHED_VECTOR_SSE4
32 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
33 #define SCHED_VECTOR_NEON
38 #define RTE_SCHED_TB_RATE_CONFIG_ERR (1e-7)
39 #define RTE_SCHED_WRR_SHIFT 3
40 #define RTE_SCHED_GRINDER_PCACHE_SIZE (64 / RTE_SCHED_QUEUES_PER_PIPE)
41 #define RTE_SCHED_PIPE_INVALID UINT32_MAX
42 #define RTE_SCHED_BMP_POS_INVALID UINT32_MAX
44 /* Scaling for cycles_per_byte calculation
45 * Chosen so that minimum rate is 480 bit/sec
47 #define RTE_SCHED_TIME_SHIFT 8
49 struct rte_sched_subport {
50 /* Token bucket (TB) */
51 uint64_t tb_time; /* time of last update */
53 uint32_t tb_credits_per_period;
57 /* Traffic classes (TCs) */
58 uint64_t tc_time; /* time of next update */
59 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
60 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
63 /* TC oversubscription */
65 uint32_t tc_ov_wm_min;
66 uint32_t tc_ov_wm_max;
67 uint8_t tc_ov_period_id;
73 struct rte_sched_subport_stats stats;
76 struct rte_sched_pipe_profile {
77 /* Token bucket (TB) */
79 uint32_t tb_credits_per_period;
82 /* Pipe traffic classes */
84 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
88 uint8_t wrr_cost[RTE_SCHED_QUEUES_PER_PIPE];
91 struct rte_sched_pipe {
92 /* Token bucket (TB) */
93 uint64_t tb_time; /* time of last update */
96 /* Pipe profile and flags */
99 /* Traffic classes (TCs) */
100 uint64_t tc_time; /* time of next update */
101 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
103 /* Weighted Round Robin (WRR) */
104 uint8_t wrr_tokens[RTE_SCHED_QUEUES_PER_PIPE];
106 /* TC oversubscription */
107 uint32_t tc_ov_credits;
108 uint8_t tc_ov_period_id;
110 } __rte_cache_aligned;
112 struct rte_sched_queue {
117 struct rte_sched_queue_extra {
118 struct rte_sched_queue_stats stats;
125 e_GRINDER_PREFETCH_PIPE = 0,
126 e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
127 e_GRINDER_PREFETCH_MBUF,
132 * Path through the scheduler hierarchy used by the scheduler enqueue
133 * operation to identify the destination queue for the current
134 * packet. Stored in the field pkt.hash.sched of struct rte_mbuf of
135 * each packet, typically written by the classification stage and read
136 * by scheduler enqueue.
138 struct rte_sched_port_hierarchy {
139 uint16_t queue:2; /**< Queue ID (0 .. 3) */
140 uint16_t traffic_class:2; /**< Traffic class ID (0 .. 3)*/
141 uint32_t color:2; /**< Color */
143 uint16_t subport; /**< Subport ID */
144 uint32_t pipe; /**< Pipe ID */
147 struct rte_sched_grinder {
149 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
150 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
155 enum grinder_state state;
158 struct rte_sched_subport *subport;
159 struct rte_sched_pipe *pipe;
160 struct rte_sched_pipe_profile *pipe_params;
163 uint8_t tccache_qmask[4];
164 uint32_t tccache_qindex[4];
170 struct rte_sched_queue *queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
171 struct rte_mbuf **qbase[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
172 uint32_t qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
176 struct rte_mbuf *pkt;
179 uint16_t wrr_tokens[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
180 uint16_t wrr_mask[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
181 uint8_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
184 struct rte_sched_port {
185 /* User parameters */
186 uint32_t n_subports_per_port;
187 uint32_t n_pipes_per_subport;
190 uint32_t frame_overhead;
191 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
192 uint32_t n_pipe_profiles;
193 uint32_t pipe_tc3_rate_max;
195 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][e_RTE_METER_COLORS];
199 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cyles */
200 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
201 uint64_t time; /* Current NIC TX time measured in bytes */
202 struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
204 /* Scheduling loop detection */
206 uint32_t pipe_exhaustion;
209 struct rte_bitmap *bmp;
210 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
213 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
214 uint32_t busy_grinders;
215 struct rte_mbuf **pkts_out;
218 /* Queue base calculation */
219 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
222 /* Large data structures */
223 struct rte_sched_subport *subport;
224 struct rte_sched_pipe *pipe;
225 struct rte_sched_queue *queue;
226 struct rte_sched_queue_extra *queue_extra;
227 struct rte_sched_pipe_profile *pipe_profiles;
229 struct rte_mbuf **queue_array;
230 uint8_t memory[0] __rte_cache_aligned;
231 } __rte_cache_aligned;
233 enum rte_sched_port_array {
234 e_RTE_SCHED_PORT_ARRAY_SUBPORT = 0,
235 e_RTE_SCHED_PORT_ARRAY_PIPE,
236 e_RTE_SCHED_PORT_ARRAY_QUEUE,
237 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA,
238 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES,
239 e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY,
240 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY,
241 e_RTE_SCHED_PORT_ARRAY_TOTAL,
244 #ifdef RTE_SCHED_COLLECT_STATS
246 static inline uint32_t
247 rte_sched_port_queues_per_subport(struct rte_sched_port *port)
249 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport;
254 static inline uint32_t
255 rte_sched_port_queues_per_port(struct rte_sched_port *port)
257 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport * port->n_subports_per_port;
260 static inline struct rte_mbuf **
261 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
263 uint32_t pindex = qindex >> 4;
264 uint32_t qpos = qindex & 0xF;
266 return (port->queue_array + pindex *
267 port->qsize_sum + port->qsize_add[qpos]);
270 static inline uint16_t
271 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
273 uint32_t tc = (qindex >> 2) & 0x3;
275 return port->qsize[tc];
279 pipe_profile_check(struct rte_sched_pipe_params *params,
284 /* Pipe parameters */
288 /* TB rate: non-zero, not greater than port rate */
289 if (params->tb_rate == 0 ||
290 params->tb_rate > rate)
293 /* TB size: non-zero */
294 if (params->tb_size == 0)
297 /* TC rate: non-zero, less than pipe rate */
298 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
299 if (params->tc_rate[i] == 0 ||
300 params->tc_rate[i] > params->tb_rate)
304 /* TC period: non-zero */
305 if (params->tc_period == 0)
308 #ifdef RTE_SCHED_SUBPORT_TC_OV
309 /* TC3 oversubscription weight: non-zero */
310 if (params->tc_ov_weight == 0)
314 /* Queue WRR weights: non-zero */
315 for (i = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
316 if (params->wrr_weights[i] == 0)
324 rte_sched_port_check_params(struct rte_sched_port_params *params)
332 if (params->socket < 0)
336 if (params->rate == 0)
340 if (params->mtu == 0)
343 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
344 if (params->n_subports_per_port == 0 ||
345 params->n_subports_per_port > 1u << 16 ||
346 !rte_is_power_of_2(params->n_subports_per_port))
349 /* n_pipes_per_subport: non-zero, power of 2 */
350 if (params->n_pipes_per_subport == 0 ||
351 !rte_is_power_of_2(params->n_pipes_per_subport))
354 /* qsize: non-zero, power of 2,
355 * no bigger than 32K (due to 16-bit read/write pointers)
357 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
358 uint16_t qsize = params->qsize[i];
360 if (qsize == 0 || !rte_is_power_of_2(qsize))
364 /* pipe_profiles and n_pipe_profiles */
365 if (params->pipe_profiles == NULL ||
366 params->n_pipe_profiles == 0 ||
367 params->n_pipe_profiles > RTE_SCHED_PIPE_PROFILES_PER_PORT)
370 for (i = 0; i < params->n_pipe_profiles; i++) {
371 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
374 status = pipe_profile_check(p, params->rate);
383 rte_sched_port_get_array_base(struct rte_sched_port_params *params, enum rte_sched_port_array array)
385 uint32_t n_subports_per_port = params->n_subports_per_port;
386 uint32_t n_pipes_per_subport = params->n_pipes_per_subport;
387 uint32_t n_pipes_per_port = n_pipes_per_subport * n_subports_per_port;
388 uint32_t n_queues_per_port = RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport * n_subports_per_port;
390 uint32_t size_subport = n_subports_per_port * sizeof(struct rte_sched_subport);
391 uint32_t size_pipe = n_pipes_per_port * sizeof(struct rte_sched_pipe);
392 uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue);
393 uint32_t size_queue_extra
394 = n_queues_per_port * sizeof(struct rte_sched_queue_extra);
395 uint32_t size_pipe_profiles
396 = RTE_SCHED_PIPE_PROFILES_PER_PORT * sizeof(struct rte_sched_pipe_profile);
397 uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port);
398 uint32_t size_per_pipe_queue_array, size_queue_array;
402 size_per_pipe_queue_array = 0;
403 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
404 size_per_pipe_queue_array += RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS
405 * params->qsize[i] * sizeof(struct rte_mbuf *);
407 size_queue_array = n_pipes_per_port * size_per_pipe_queue_array;
411 if (array == e_RTE_SCHED_PORT_ARRAY_SUBPORT)
413 base += RTE_CACHE_LINE_ROUNDUP(size_subport);
415 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE)
417 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
419 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE)
421 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
423 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA)
425 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
427 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES)
429 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
431 if (array == e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY)
433 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
435 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY)
437 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
443 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
445 uint32_t size0, size1;
448 status = rte_sched_port_check_params(params);
450 RTE_LOG(NOTICE, SCHED,
451 "Port scheduler params check failed (%d)\n", status);
456 size0 = sizeof(struct rte_sched_port);
457 size1 = rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_TOTAL);
459 return size0 + size1;
463 rte_sched_port_config_qsize(struct rte_sched_port *port)
466 port->qsize_add[0] = 0;
467 port->qsize_add[1] = port->qsize_add[0] + port->qsize[0];
468 port->qsize_add[2] = port->qsize_add[1] + port->qsize[0];
469 port->qsize_add[3] = port->qsize_add[2] + port->qsize[0];
472 port->qsize_add[4] = port->qsize_add[3] + port->qsize[0];
473 port->qsize_add[5] = port->qsize_add[4] + port->qsize[1];
474 port->qsize_add[6] = port->qsize_add[5] + port->qsize[1];
475 port->qsize_add[7] = port->qsize_add[6] + port->qsize[1];
478 port->qsize_add[8] = port->qsize_add[7] + port->qsize[1];
479 port->qsize_add[9] = port->qsize_add[8] + port->qsize[2];
480 port->qsize_add[10] = port->qsize_add[9] + port->qsize[2];
481 port->qsize_add[11] = port->qsize_add[10] + port->qsize[2];
484 port->qsize_add[12] = port->qsize_add[11] + port->qsize[2];
485 port->qsize_add[13] = port->qsize_add[12] + port->qsize[3];
486 port->qsize_add[14] = port->qsize_add[13] + port->qsize[3];
487 port->qsize_add[15] = port->qsize_add[14] + port->qsize[3];
489 port->qsize_sum = port->qsize_add[15] + port->qsize[3];
493 rte_sched_port_log_pipe_profile(struct rte_sched_port *port, uint32_t i)
495 struct rte_sched_pipe_profile *p = port->pipe_profiles + i;
497 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
498 " Token bucket: period = %u, credits per period = %u, size = %u\n"
499 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
500 " Traffic class 3 oversubscription: weight = %hhu\n"
501 " WRR cost: [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu]\n",
506 p->tb_credits_per_period,
509 /* Traffic classes */
511 p->tc_credits_per_period[0],
512 p->tc_credits_per_period[1],
513 p->tc_credits_per_period[2],
514 p->tc_credits_per_period[3],
516 /* Traffic class 3 oversubscription */
520 p->wrr_cost[ 0], p->wrr_cost[ 1], p->wrr_cost[ 2], p->wrr_cost[ 3],
521 p->wrr_cost[ 4], p->wrr_cost[ 5], p->wrr_cost[ 6], p->wrr_cost[ 7],
522 p->wrr_cost[ 8], p->wrr_cost[ 9], p->wrr_cost[10], p->wrr_cost[11],
523 p->wrr_cost[12], p->wrr_cost[13], p->wrr_cost[14], p->wrr_cost[15]);
526 static inline uint64_t
527 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
529 uint64_t time = time_ms;
531 time = (time * rate) / 1000;
537 rte_sched_pipe_profile_convert(struct rte_sched_pipe_params *src,
538 struct rte_sched_pipe_profile *dst,
544 if (src->tb_rate == rate) {
545 dst->tb_credits_per_period = 1;
548 double tb_rate = (double) src->tb_rate
550 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
552 rte_approx(tb_rate, d,
553 &dst->tb_credits_per_period, &dst->tb_period);
556 dst->tb_size = src->tb_size;
558 /* Traffic Classes */
559 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
562 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
563 dst->tc_credits_per_period[i]
564 = rte_sched_time_ms_to_bytes(src->tc_period,
567 #ifdef RTE_SCHED_SUBPORT_TC_OV
568 dst->tc_ov_weight = src->tc_ov_weight;
572 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
573 uint32_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
574 uint32_t lcd, lcd1, lcd2;
577 qindex = i * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS;
579 wrr_cost[0] = src->wrr_weights[qindex];
580 wrr_cost[1] = src->wrr_weights[qindex + 1];
581 wrr_cost[2] = src->wrr_weights[qindex + 2];
582 wrr_cost[3] = src->wrr_weights[qindex + 3];
584 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
585 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
586 lcd = rte_get_lcd(lcd1, lcd2);
588 wrr_cost[0] = lcd / wrr_cost[0];
589 wrr_cost[1] = lcd / wrr_cost[1];
590 wrr_cost[2] = lcd / wrr_cost[2];
591 wrr_cost[3] = lcd / wrr_cost[3];
593 dst->wrr_cost[qindex] = (uint8_t) wrr_cost[0];
594 dst->wrr_cost[qindex + 1] = (uint8_t) wrr_cost[1];
595 dst->wrr_cost[qindex + 2] = (uint8_t) wrr_cost[2];
596 dst->wrr_cost[qindex + 3] = (uint8_t) wrr_cost[3];
601 rte_sched_port_config_pipe_profile_table(struct rte_sched_port *port,
602 struct rte_sched_port_params *params)
606 for (i = 0; i < port->n_pipe_profiles; i++) {
607 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
608 struct rte_sched_pipe_profile *dst = port->pipe_profiles + i;
610 rte_sched_pipe_profile_convert(src, dst, params->rate);
611 rte_sched_port_log_pipe_profile(port, i);
614 port->pipe_tc3_rate_max = 0;
615 for (i = 0; i < port->n_pipe_profiles; i++) {
616 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
617 uint32_t pipe_tc3_rate = src->tc_rate[3];
619 if (port->pipe_tc3_rate_max < pipe_tc3_rate)
620 port->pipe_tc3_rate_max = pipe_tc3_rate;
624 struct rte_sched_port *
625 rte_sched_port_config(struct rte_sched_port_params *params)
627 struct rte_sched_port *port = NULL;
628 uint32_t mem_size, bmp_mem_size, n_queues_per_port, i, cycles_per_byte;
630 /* Check user parameters. Determine the amount of memory to allocate */
631 mem_size = rte_sched_port_get_memory_footprint(params);
635 /* Allocate memory to store the data structures */
636 port = rte_zmalloc_socket("qos_params", mem_size, RTE_CACHE_LINE_SIZE,
641 /* compile time checks */
642 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
643 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS & (RTE_SCHED_PORT_N_GRINDERS - 1));
645 /* User parameters */
646 port->n_subports_per_port = params->n_subports_per_port;
647 port->n_pipes_per_subport = params->n_pipes_per_subport;
648 port->rate = params->rate;
649 port->mtu = params->mtu + params->frame_overhead;
650 port->frame_overhead = params->frame_overhead;
651 memcpy(port->qsize, params->qsize, sizeof(params->qsize));
652 port->n_pipe_profiles = params->n_pipe_profiles;
655 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
658 for (j = 0; j < e_RTE_METER_COLORS; j++) {
659 /* if min/max are both zero, then RED is disabled */
660 if ((params->red_params[i][j].min_th |
661 params->red_params[i][j].max_th) == 0) {
665 if (rte_red_config_init(&port->red_config[i][j],
666 params->red_params[i][j].wq_log2,
667 params->red_params[i][j].min_th,
668 params->red_params[i][j].max_th,
669 params->red_params[i][j].maxp_inv) != 0) {
677 port->time_cpu_cycles = rte_get_tsc_cycles();
678 port->time_cpu_bytes = 0;
681 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
683 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
685 /* Scheduling loop detection */
686 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
687 port->pipe_exhaustion = 0;
690 port->busy_grinders = 0;
691 port->pkts_out = NULL;
692 port->n_pkts_out = 0;
694 /* Queue base calculation */
695 rte_sched_port_config_qsize(port);
697 /* Large data structures */
698 port->subport = (struct rte_sched_subport *)
699 (port->memory + rte_sched_port_get_array_base(params,
700 e_RTE_SCHED_PORT_ARRAY_SUBPORT));
701 port->pipe = (struct rte_sched_pipe *)
702 (port->memory + rte_sched_port_get_array_base(params,
703 e_RTE_SCHED_PORT_ARRAY_PIPE));
704 port->queue = (struct rte_sched_queue *)
705 (port->memory + rte_sched_port_get_array_base(params,
706 e_RTE_SCHED_PORT_ARRAY_QUEUE));
707 port->queue_extra = (struct rte_sched_queue_extra *)
708 (port->memory + rte_sched_port_get_array_base(params,
709 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA));
710 port->pipe_profiles = (struct rte_sched_pipe_profile *)
711 (port->memory + rte_sched_port_get_array_base(params,
712 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES));
713 port->bmp_array = port->memory
714 + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY);
715 port->queue_array = (struct rte_mbuf **)
716 (port->memory + rte_sched_port_get_array_base(params,
717 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY));
719 /* Pipe profile table */
720 rte_sched_port_config_pipe_profile_table(port, params);
723 n_queues_per_port = rte_sched_port_queues_per_port(port);
724 bmp_mem_size = rte_bitmap_get_memory_footprint(n_queues_per_port);
725 port->bmp = rte_bitmap_init(n_queues_per_port, port->bmp_array,
727 if (port->bmp == NULL) {
728 RTE_LOG(ERR, SCHED, "Bitmap init error\n");
732 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
733 port->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
740 rte_sched_port_free(struct rte_sched_port *port)
743 uint32_t n_queues_per_port;
745 /* Check user parameters */
749 n_queues_per_port = rte_sched_port_queues_per_port(port);
751 /* Free enqueued mbufs */
752 for (qindex = 0; qindex < n_queues_per_port; qindex++) {
753 struct rte_mbuf **mbufs = rte_sched_port_qbase(port, qindex);
754 uint16_t qsize = rte_sched_port_qsize(port, qindex);
755 struct rte_sched_queue *queue = port->queue + qindex;
756 uint16_t qr = queue->qr & (qsize - 1);
757 uint16_t qw = queue->qw & (qsize - 1);
759 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
760 rte_pktmbuf_free(mbufs[qr]);
763 rte_bitmap_free(port->bmp);
768 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
770 struct rte_sched_subport *s = port->subport + i;
772 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
773 " Token bucket: period = %u, credits per period = %u, size = %u\n"
774 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
775 " Traffic class 3 oversubscription: wm min = %u, wm max = %u\n",
780 s->tb_credits_per_period,
783 /* Traffic classes */
785 s->tc_credits_per_period[0],
786 s->tc_credits_per_period[1],
787 s->tc_credits_per_period[2],
788 s->tc_credits_per_period[3],
790 /* Traffic class 3 oversubscription */
796 rte_sched_subport_config(struct rte_sched_port *port,
798 struct rte_sched_subport_params *params)
800 struct rte_sched_subport *s;
803 /* Check user parameters */
805 subport_id >= port->n_subports_per_port ||
809 if (params->tb_rate == 0 || params->tb_rate > port->rate)
812 if (params->tb_size == 0)
815 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
816 if (params->tc_rate[i] == 0 ||
817 params->tc_rate[i] > params->tb_rate)
821 if (params->tc_period == 0)
824 s = port->subport + subport_id;
826 /* Token Bucket (TB) */
827 if (params->tb_rate == port->rate) {
828 s->tb_credits_per_period = 1;
831 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
832 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
834 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
837 s->tb_size = params->tb_size;
838 s->tb_time = port->time;
839 s->tb_credits = s->tb_size / 2;
841 /* Traffic Classes (TCs) */
842 s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
843 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
844 s->tc_credits_per_period[i]
845 = rte_sched_time_ms_to_bytes(params->tc_period,
848 s->tc_time = port->time + s->tc_period;
849 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
850 s->tc_credits[i] = s->tc_credits_per_period[i];
852 #ifdef RTE_SCHED_SUBPORT_TC_OV
853 /* TC oversubscription */
854 s->tc_ov_wm_min = port->mtu;
855 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
856 port->pipe_tc3_rate_max);
857 s->tc_ov_wm = s->tc_ov_wm_max;
858 s->tc_ov_period_id = 0;
864 rte_sched_port_log_subport_config(port, subport_id);
870 rte_sched_pipe_config(struct rte_sched_port *port,
873 int32_t pipe_profile)
875 struct rte_sched_subport *s;
876 struct rte_sched_pipe *p;
877 struct rte_sched_pipe_profile *params;
878 uint32_t deactivate, profile, i;
880 /* Check user parameters */
881 profile = (uint32_t) pipe_profile;
882 deactivate = (pipe_profile < 0);
885 subport_id >= port->n_subports_per_port ||
886 pipe_id >= port->n_pipes_per_subport ||
887 (!deactivate && profile >= port->n_pipe_profiles))
891 /* Check that subport configuration is valid */
892 s = port->subport + subport_id;
893 if (s->tb_period == 0)
896 p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
898 /* Handle the case when pipe already has a valid configuration */
900 params = port->pipe_profiles + p->profile;
902 #ifdef RTE_SCHED_SUBPORT_TC_OV
903 double subport_tc3_rate = (double) s->tc_credits_per_period[3]
904 / (double) s->tc_period;
905 double pipe_tc3_rate = (double) params->tc_credits_per_period[3]
906 / (double) params->tc_period;
907 uint32_t tc3_ov = s->tc_ov;
909 /* Unplug pipe from its subport */
910 s->tc_ov_n -= params->tc_ov_weight;
911 s->tc_ov_rate -= pipe_tc3_rate;
912 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
914 if (s->tc_ov != tc3_ov) {
915 RTE_LOG(DEBUG, SCHED,
916 "Subport %u TC3 oversubscription is OFF (%.4lf >= %.4lf)\n",
917 subport_id, subport_tc3_rate, s->tc_ov_rate);
922 memset(p, 0, sizeof(struct rte_sched_pipe));
928 /* Apply the new pipe configuration */
929 p->profile = profile;
930 params = port->pipe_profiles + p->profile;
932 /* Token Bucket (TB) */
933 p->tb_time = port->time;
934 p->tb_credits = params->tb_size / 2;
936 /* Traffic Classes (TCs) */
937 p->tc_time = port->time + params->tc_period;
938 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
939 p->tc_credits[i] = params->tc_credits_per_period[i];
941 #ifdef RTE_SCHED_SUBPORT_TC_OV
943 /* Subport TC3 oversubscription */
944 double subport_tc3_rate = (double) s->tc_credits_per_period[3]
945 / (double) s->tc_period;
946 double pipe_tc3_rate = (double) params->tc_credits_per_period[3]
947 / (double) params->tc_period;
948 uint32_t tc3_ov = s->tc_ov;
950 s->tc_ov_n += params->tc_ov_weight;
951 s->tc_ov_rate += pipe_tc3_rate;
952 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
954 if (s->tc_ov != tc3_ov) {
955 RTE_LOG(DEBUG, SCHED,
956 "Subport %u TC3 oversubscription is ON (%.4lf < %.4lf)\n",
957 subport_id, subport_tc3_rate, s->tc_ov_rate);
959 p->tc_ov_period_id = s->tc_ov_period_id;
960 p->tc_ov_credits = s->tc_ov_wm;
967 int __rte_experimental
968 rte_sched_port_pipe_profile_add(struct rte_sched_port *port,
969 struct rte_sched_pipe_params *params,
970 uint32_t *pipe_profile_id)
972 struct rte_sched_pipe_profile *pp;
980 /* Pipe profiles not exceeds the max limit */
981 if (port->n_pipe_profiles >= RTE_SCHED_PIPE_PROFILES_PER_PORT)
985 status = pipe_profile_check(params, port->rate);
989 pp = &port->pipe_profiles[port->n_pipe_profiles];
990 rte_sched_pipe_profile_convert(params, pp, port->rate);
992 /* Pipe profile not exists */
993 for (i = 0; i < port->n_pipe_profiles; i++)
994 if (memcmp(port->pipe_profiles + i, pp, sizeof(*pp)) == 0)
997 /* Pipe profile commit */
998 *pipe_profile_id = port->n_pipe_profiles;
999 port->n_pipe_profiles++;
1001 if (port->pipe_tc3_rate_max < params->tc_rate[3])
1002 port->pipe_tc3_rate_max = params->tc_rate[3];
1004 rte_sched_port_log_pipe_profile(port, *pipe_profile_id);
1010 rte_sched_port_pkt_write(struct rte_mbuf *pkt,
1011 uint32_t subport, uint32_t pipe, uint32_t traffic_class,
1012 uint32_t queue, enum rte_meter_color color)
1014 struct rte_sched_port_hierarchy *sched
1015 = (struct rte_sched_port_hierarchy *) &pkt->hash.sched;
1017 RTE_BUILD_BUG_ON(sizeof(*sched) > sizeof(pkt->hash.sched));
1019 sched->color = (uint32_t) color;
1020 sched->subport = subport;
1022 sched->traffic_class = traffic_class;
1023 sched->queue = queue;
1027 rte_sched_port_pkt_read_tree_path(const struct rte_mbuf *pkt,
1028 uint32_t *subport, uint32_t *pipe,
1029 uint32_t *traffic_class, uint32_t *queue)
1031 const struct rte_sched_port_hierarchy *sched
1032 = (const struct rte_sched_port_hierarchy *) &pkt->hash.sched;
1034 *subport = sched->subport;
1035 *pipe = sched->pipe;
1036 *traffic_class = sched->traffic_class;
1037 *queue = sched->queue;
1040 enum rte_meter_color
1041 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1043 const struct rte_sched_port_hierarchy *sched
1044 = (const struct rte_sched_port_hierarchy *) &pkt->hash.sched;
1046 return (enum rte_meter_color) sched->color;
1050 rte_sched_subport_read_stats(struct rte_sched_port *port,
1051 uint32_t subport_id,
1052 struct rte_sched_subport_stats *stats,
1055 struct rte_sched_subport *s;
1057 /* Check user parameters */
1058 if (port == NULL || subport_id >= port->n_subports_per_port ||
1059 stats == NULL || tc_ov == NULL)
1062 s = port->subport + subport_id;
1064 /* Copy subport stats and clear */
1065 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1066 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1068 /* Subport TC oversubscription status */
1075 rte_sched_queue_read_stats(struct rte_sched_port *port,
1077 struct rte_sched_queue_stats *stats,
1080 struct rte_sched_queue *q;
1081 struct rte_sched_queue_extra *qe;
1083 /* Check user parameters */
1084 if ((port == NULL) ||
1085 (queue_id >= rte_sched_port_queues_per_port(port)) ||
1090 q = port->queue + queue_id;
1091 qe = port->queue_extra + queue_id;
1093 /* Copy queue stats and clear */
1094 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1095 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1098 *qlen = q->qw - q->qr;
1103 static inline uint32_t
1104 rte_sched_port_qindex(struct rte_sched_port *port, uint32_t subport, uint32_t pipe, uint32_t traffic_class, uint32_t queue)
1108 result = subport * port->n_pipes_per_subport + pipe;
1109 result = result * RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE + traffic_class;
1110 result = result * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
1115 #ifdef RTE_SCHED_DEBUG
1118 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1120 struct rte_sched_queue *queue = port->queue + qindex;
1122 return queue->qr == queue->qw;
1125 #endif /* RTE_SCHED_DEBUG */
1127 #ifdef RTE_SCHED_COLLECT_STATS
1130 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1132 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1133 uint32_t tc_index = (qindex >> 2) & 0x3;
1134 uint32_t pkt_len = pkt->pkt_len;
1136 s->stats.n_pkts_tc[tc_index] += 1;
1137 s->stats.n_bytes_tc[tc_index] += pkt_len;
1140 #ifdef RTE_SCHED_RED
1142 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1144 struct rte_mbuf *pkt, uint32_t red)
1147 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1149 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1152 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1153 uint32_t tc_index = (qindex >> 2) & 0x3;
1154 uint32_t pkt_len = pkt->pkt_len;
1156 s->stats.n_pkts_tc_dropped[tc_index] += 1;
1157 s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1158 #ifdef RTE_SCHED_RED
1159 s->stats.n_pkts_red_dropped[tc_index] += red;
1164 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1166 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1167 uint32_t pkt_len = pkt->pkt_len;
1169 qe->stats.n_pkts += 1;
1170 qe->stats.n_bytes += pkt_len;
1173 #ifdef RTE_SCHED_RED
1175 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1177 struct rte_mbuf *pkt, uint32_t red)
1180 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1182 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1185 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1186 uint32_t pkt_len = pkt->pkt_len;
1188 qe->stats.n_pkts_dropped += 1;
1189 qe->stats.n_bytes_dropped += pkt_len;
1190 #ifdef RTE_SCHED_RED
1191 qe->stats.n_pkts_red_dropped += red;
1195 #endif /* RTE_SCHED_COLLECT_STATS */
1197 #ifdef RTE_SCHED_RED
1200 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1202 struct rte_sched_queue_extra *qe;
1203 struct rte_red_config *red_cfg;
1204 struct rte_red *red;
1206 enum rte_meter_color color;
1208 tc_index = (qindex >> 2) & 0x3;
1209 color = rte_sched_port_pkt_read_color(pkt);
1210 red_cfg = &port->red_config[tc_index][color];
1212 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1215 qe = port->queue_extra + qindex;
1218 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1222 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1224 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1225 struct rte_red *red = &qe->red;
1227 rte_red_mark_queue_empty(red, port->time);
1232 #define rte_sched_port_red_drop(port, pkt, qindex, qlen) 0
1234 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1236 #endif /* RTE_SCHED_RED */
1238 #ifdef RTE_SCHED_DEBUG
1241 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1248 rte_panic("Empty slab at position %u\n", bmp_pos);
1251 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1252 if (mask & bmp_slab) {
1253 if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1254 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1261 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1265 #endif /* RTE_SCHED_DEBUG */
1267 static inline uint32_t
1268 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port,
1269 struct rte_mbuf *pkt)
1271 struct rte_sched_queue *q;
1272 #ifdef RTE_SCHED_COLLECT_STATS
1273 struct rte_sched_queue_extra *qe;
1275 uint32_t subport, pipe, traffic_class, queue, qindex;
1277 rte_sched_port_pkt_read_tree_path(pkt, &subport, &pipe, &traffic_class, &queue);
1279 qindex = rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1280 q = port->queue + qindex;
1282 #ifdef RTE_SCHED_COLLECT_STATS
1283 qe = port->queue_extra + qindex;
1291 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1292 uint32_t qindex, struct rte_mbuf **qbase)
1294 struct rte_sched_queue *q;
1295 struct rte_mbuf **q_qw;
1298 q = port->queue + qindex;
1299 qsize = rte_sched_port_qsize(port, qindex);
1300 q_qw = qbase + (q->qw & (qsize - 1));
1302 rte_prefetch0(q_qw);
1303 rte_bitmap_prefetch0(port->bmp, qindex);
1307 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex,
1308 struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1310 struct rte_sched_queue *q;
1314 q = port->queue + qindex;
1315 qsize = rte_sched_port_qsize(port, qindex);
1316 qlen = q->qw - q->qr;
1318 /* Drop the packet (and update drop stats) when queue is full */
1319 if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) ||
1321 rte_pktmbuf_free(pkt);
1322 #ifdef RTE_SCHED_COLLECT_STATS
1323 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt,
1325 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt,
1331 /* Enqueue packet */
1332 qbase[q->qw & (qsize - 1)] = pkt;
1335 /* Activate queue in the port bitmap */
1336 rte_bitmap_set(port->bmp, qindex);
1339 #ifdef RTE_SCHED_COLLECT_STATS
1340 rte_sched_port_update_subport_stats(port, qindex, pkt);
1341 rte_sched_port_update_queue_stats(port, qindex, pkt);
1349 * The enqueue function implements a 4-level pipeline with each stage
1350 * processing two different packets. The purpose of using a pipeline
1351 * is to hide the latency of prefetching the data structures. The
1352 * naming convention is presented in the diagram below:
1354 * p00 _______ p10 _______ p20 _______ p30 _______
1355 * ----->| |----->| |----->| |----->| |----->
1356 * | 0 | | 1 | | 2 | | 3 |
1357 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1362 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1365 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1366 *pkt30, *pkt31, *pkt_last;
1367 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1368 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1369 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1370 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1376 * Less then 6 input packets available, which is not enough to
1379 if (unlikely(n_pkts < 6)) {
1380 struct rte_mbuf **q_base[5];
1383 /* Prefetch the mbuf structure of each packet */
1384 for (i = 0; i < n_pkts; i++)
1385 rte_prefetch0(pkts[i]);
1387 /* Prefetch the queue structure for each queue */
1388 for (i = 0; i < n_pkts; i++)
1389 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port,
1392 /* Prefetch the write pointer location of each queue */
1393 for (i = 0; i < n_pkts; i++) {
1394 q_base[i] = rte_sched_port_qbase(port, q[i]);
1395 rte_sched_port_enqueue_qwa_prefetch0(port, q[i],
1399 /* Write each packet to its queue */
1400 for (i = 0; i < n_pkts; i++)
1401 result += rte_sched_port_enqueue_qwa(port, q[i],
1402 q_base[i], pkts[i]);
1407 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1410 rte_prefetch0(pkt20);
1411 rte_prefetch0(pkt21);
1415 rte_prefetch0(pkt10);
1416 rte_prefetch0(pkt11);
1418 q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1419 q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1423 rte_prefetch0(pkt00);
1424 rte_prefetch0(pkt01);
1426 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1427 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1429 q20_base = rte_sched_port_qbase(port, q20);
1430 q21_base = rte_sched_port_qbase(port, q21);
1431 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1432 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1434 /* Run the pipeline */
1435 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1436 /* Propagate stage inputs */
1447 q30_base = q20_base;
1448 q31_base = q21_base;
1450 /* Stage 0: Get packets in */
1452 pkt01 = pkts[i + 1];
1453 rte_prefetch0(pkt00);
1454 rte_prefetch0(pkt01);
1456 /* Stage 1: Prefetch queue structure storing queue pointers */
1457 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1458 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1460 /* Stage 2: Prefetch queue write location */
1461 q20_base = rte_sched_port_qbase(port, q20);
1462 q21_base = rte_sched_port_qbase(port, q21);
1463 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1464 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1466 /* Stage 3: Write packet to queue and activate queue */
1467 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1468 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1469 result += r30 + r31;
1473 * Drain the pipeline (exactly 6 packets).
1474 * Handle the last packet in the case
1475 * of an odd number of input packets.
1477 pkt_last = pkts[n_pkts - 1];
1478 rte_prefetch0(pkt_last);
1480 q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1481 q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1483 q10_base = rte_sched_port_qbase(port, q10);
1484 q11_base = rte_sched_port_qbase(port, q11);
1485 rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1486 rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1488 r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1489 r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1490 result += r20 + r21;
1492 q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1494 q00_base = rte_sched_port_qbase(port, q00);
1495 q01_base = rte_sched_port_qbase(port, q01);
1496 rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1497 rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1499 r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1500 r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1501 result += r10 + r11;
1503 q_last_base = rte_sched_port_qbase(port, q_last);
1504 rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1506 r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1507 r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1508 result += r00 + r01;
1511 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1518 #ifndef RTE_SCHED_SUBPORT_TC_OV
1521 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1523 struct rte_sched_grinder *grinder = port->grinder + pos;
1524 struct rte_sched_subport *subport = grinder->subport;
1525 struct rte_sched_pipe *pipe = grinder->pipe;
1526 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1530 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1531 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1532 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1533 subport->tb_time += n_periods * subport->tb_period;
1536 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1537 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1538 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1539 pipe->tb_time += n_periods * params->tb_period;
1542 if (unlikely(port->time >= subport->tc_time)) {
1543 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1544 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1545 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1546 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1547 subport->tc_time = port->time + subport->tc_period;
1551 if (unlikely(port->time >= pipe->tc_time)) {
1552 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1553 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1554 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1555 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1556 pipe->tc_time = port->time + params->tc_period;
1562 static inline uint32_t
1563 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1565 struct rte_sched_grinder *grinder = port->grinder + pos;
1566 struct rte_sched_subport *subport = grinder->subport;
1567 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1568 uint32_t tc_ov_consumption_max;
1569 uint32_t tc_ov_wm = subport->tc_ov_wm;
1571 if (subport->tc_ov == 0)
1572 return subport->tc_ov_wm_max;
1574 tc_ov_consumption[0] = subport->tc_credits_per_period[0] - subport->tc_credits[0];
1575 tc_ov_consumption[1] = subport->tc_credits_per_period[1] - subport->tc_credits[1];
1576 tc_ov_consumption[2] = subport->tc_credits_per_period[2] - subport->tc_credits[2];
1577 tc_ov_consumption[3] = subport->tc_credits_per_period[3] - subport->tc_credits[3];
1579 tc_ov_consumption_max = subport->tc_credits_per_period[3] -
1580 (tc_ov_consumption[0] + tc_ov_consumption[1] + tc_ov_consumption[2]);
1582 if (tc_ov_consumption[3] > (tc_ov_consumption_max - port->mtu)) {
1583 tc_ov_wm -= tc_ov_wm >> 7;
1584 if (tc_ov_wm < subport->tc_ov_wm_min)
1585 tc_ov_wm = subport->tc_ov_wm_min;
1590 tc_ov_wm += (tc_ov_wm >> 7) + 1;
1591 if (tc_ov_wm > subport->tc_ov_wm_max)
1592 tc_ov_wm = subport->tc_ov_wm_max;
1598 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1600 struct rte_sched_grinder *grinder = port->grinder + pos;
1601 struct rte_sched_subport *subport = grinder->subport;
1602 struct rte_sched_pipe *pipe = grinder->pipe;
1603 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1607 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1608 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1609 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1610 subport->tb_time += n_periods * subport->tb_period;
1613 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1614 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1615 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1616 pipe->tb_time += n_periods * params->tb_period;
1619 if (unlikely(port->time >= subport->tc_time)) {
1620 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
1622 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1623 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1624 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1625 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1627 subport->tc_time = port->time + subport->tc_period;
1628 subport->tc_ov_period_id++;
1632 if (unlikely(port->time >= pipe->tc_time)) {
1633 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1634 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1635 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1636 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1637 pipe->tc_time = port->time + params->tc_period;
1640 /* Pipe TCs - Oversubscription */
1641 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
1642 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
1644 pipe->tc_ov_period_id = subport->tc_ov_period_id;
1648 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
1651 #ifndef RTE_SCHED_SUBPORT_TC_OV
1654 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1656 struct rte_sched_grinder *grinder = port->grinder + pos;
1657 struct rte_sched_subport *subport = grinder->subport;
1658 struct rte_sched_pipe *pipe = grinder->pipe;
1659 struct rte_mbuf *pkt = grinder->pkt;
1660 uint32_t tc_index = grinder->tc_index;
1661 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1662 uint32_t subport_tb_credits = subport->tb_credits;
1663 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1664 uint32_t pipe_tb_credits = pipe->tb_credits;
1665 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1668 /* Check queue credits */
1669 enough_credits = (pkt_len <= subport_tb_credits) &&
1670 (pkt_len <= subport_tc_credits) &&
1671 (pkt_len <= pipe_tb_credits) &&
1672 (pkt_len <= pipe_tc_credits);
1674 if (!enough_credits)
1677 /* Update port credits */
1678 subport->tb_credits -= pkt_len;
1679 subport->tc_credits[tc_index] -= pkt_len;
1680 pipe->tb_credits -= pkt_len;
1681 pipe->tc_credits[tc_index] -= pkt_len;
1689 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1691 struct rte_sched_grinder *grinder = port->grinder + pos;
1692 struct rte_sched_subport *subport = grinder->subport;
1693 struct rte_sched_pipe *pipe = grinder->pipe;
1694 struct rte_mbuf *pkt = grinder->pkt;
1695 uint32_t tc_index = grinder->tc_index;
1696 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1697 uint32_t subport_tb_credits = subport->tb_credits;
1698 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1699 uint32_t pipe_tb_credits = pipe->tb_credits;
1700 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1701 uint32_t pipe_tc_ov_mask1[] = {UINT32_MAX, UINT32_MAX, UINT32_MAX, pipe->tc_ov_credits};
1702 uint32_t pipe_tc_ov_mask2[] = {0, 0, 0, UINT32_MAX};
1703 uint32_t pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
1706 /* Check pipe and subport credits */
1707 enough_credits = (pkt_len <= subport_tb_credits) &&
1708 (pkt_len <= subport_tc_credits) &&
1709 (pkt_len <= pipe_tb_credits) &&
1710 (pkt_len <= pipe_tc_credits) &&
1711 (pkt_len <= pipe_tc_ov_credits);
1713 if (!enough_credits)
1716 /* Update pipe and subport credits */
1717 subport->tb_credits -= pkt_len;
1718 subport->tc_credits[tc_index] -= pkt_len;
1719 pipe->tb_credits -= pkt_len;
1720 pipe->tc_credits[tc_index] -= pkt_len;
1721 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
1726 #endif /* RTE_SCHED_SUBPORT_TC_OV */
1730 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
1732 struct rte_sched_grinder *grinder = port->grinder + pos;
1733 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
1734 struct rte_mbuf *pkt = grinder->pkt;
1735 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1737 if (!grinder_credits_check(port, pos))
1740 /* Advance port time */
1741 port->time += pkt_len;
1744 port->pkts_out[port->n_pkts_out++] = pkt;
1746 grinder->wrr_tokens[grinder->qpos] += pkt_len * grinder->wrr_cost[grinder->qpos];
1747 if (queue->qr == queue->qw) {
1748 uint32_t qindex = grinder->qindex[grinder->qpos];
1750 rte_bitmap_clear(port->bmp, qindex);
1751 grinder->qmask &= ~(1 << grinder->qpos);
1752 grinder->wrr_mask[grinder->qpos] = 0;
1753 rte_sched_port_set_queue_empty_timestamp(port, qindex);
1756 /* Reset pipe loop detection */
1757 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1758 grinder->productive = 1;
1763 #ifdef SCHED_VECTOR_SSE4
1766 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1768 __m128i index = _mm_set1_epi32(base_pipe);
1769 __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
1770 __m128i res = _mm_cmpeq_epi32(pipes, index);
1772 pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
1773 pipes = _mm_cmpeq_epi32(pipes, index);
1774 res = _mm_or_si128(res, pipes);
1776 if (_mm_testz_si128(res, res))
1782 #elif defined(SCHED_VECTOR_NEON)
1785 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1787 uint32x4_t index, pipes;
1788 uint32_t *pos = (uint32_t *)port->grinder_base_bmp_pos;
1790 index = vmovq_n_u32(base_pipe);
1791 pipes = vld1q_u32(pos);
1792 if (!vminvq_u32(veorq_u32(pipes, index)))
1795 pipes = vld1q_u32(pos + 4);
1796 if (!vminvq_u32(veorq_u32(pipes, index)))
1805 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1809 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
1810 if (port->grinder_base_bmp_pos[i] == base_pipe)
1817 #endif /* RTE_SCHED_OPTIMIZATIONS */
1820 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
1822 struct rte_sched_grinder *grinder = port->grinder + pos;
1825 grinder->pcache_w = 0;
1826 grinder->pcache_r = 0;
1828 w[0] = (uint16_t) bmp_slab;
1829 w[1] = (uint16_t) (bmp_slab >> 16);
1830 w[2] = (uint16_t) (bmp_slab >> 32);
1831 w[3] = (uint16_t) (bmp_slab >> 48);
1833 grinder->pcache_qmask[grinder->pcache_w] = w[0];
1834 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
1835 grinder->pcache_w += (w[0] != 0);
1837 grinder->pcache_qmask[grinder->pcache_w] = w[1];
1838 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
1839 grinder->pcache_w += (w[1] != 0);
1841 grinder->pcache_qmask[grinder->pcache_w] = w[2];
1842 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
1843 grinder->pcache_w += (w[2] != 0);
1845 grinder->pcache_qmask[grinder->pcache_w] = w[3];
1846 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
1847 grinder->pcache_w += (w[3] != 0);
1851 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
1853 struct rte_sched_grinder *grinder = port->grinder + pos;
1856 grinder->tccache_w = 0;
1857 grinder->tccache_r = 0;
1859 b[0] = (uint8_t) (qmask & 0xF);
1860 b[1] = (uint8_t) ((qmask >> 4) & 0xF);
1861 b[2] = (uint8_t) ((qmask >> 8) & 0xF);
1862 b[3] = (uint8_t) ((qmask >> 12) & 0xF);
1864 grinder->tccache_qmask[grinder->tccache_w] = b[0];
1865 grinder->tccache_qindex[grinder->tccache_w] = qindex;
1866 grinder->tccache_w += (b[0] != 0);
1868 grinder->tccache_qmask[grinder->tccache_w] = b[1];
1869 grinder->tccache_qindex[grinder->tccache_w] = qindex + 4;
1870 grinder->tccache_w += (b[1] != 0);
1872 grinder->tccache_qmask[grinder->tccache_w] = b[2];
1873 grinder->tccache_qindex[grinder->tccache_w] = qindex + 8;
1874 grinder->tccache_w += (b[2] != 0);
1876 grinder->tccache_qmask[grinder->tccache_w] = b[3];
1877 grinder->tccache_qindex[grinder->tccache_w] = qindex + 12;
1878 grinder->tccache_w += (b[3] != 0);
1882 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
1884 struct rte_sched_grinder *grinder = port->grinder + pos;
1885 struct rte_mbuf **qbase;
1889 if (grinder->tccache_r == grinder->tccache_w)
1892 qindex = grinder->tccache_qindex[grinder->tccache_r];
1893 qbase = rte_sched_port_qbase(port, qindex);
1894 qsize = rte_sched_port_qsize(port, qindex);
1896 grinder->tc_index = (qindex >> 2) & 0x3;
1897 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
1898 grinder->qsize = qsize;
1900 grinder->qindex[0] = qindex;
1901 grinder->qindex[1] = qindex + 1;
1902 grinder->qindex[2] = qindex + 2;
1903 grinder->qindex[3] = qindex + 3;
1905 grinder->queue[0] = port->queue + qindex;
1906 grinder->queue[1] = port->queue + qindex + 1;
1907 grinder->queue[2] = port->queue + qindex + 2;
1908 grinder->queue[3] = port->queue + qindex + 3;
1910 grinder->qbase[0] = qbase;
1911 grinder->qbase[1] = qbase + qsize;
1912 grinder->qbase[2] = qbase + 2 * qsize;
1913 grinder->qbase[3] = qbase + 3 * qsize;
1915 grinder->tccache_r++;
1920 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
1922 struct rte_sched_grinder *grinder = port->grinder + pos;
1923 uint32_t pipe_qindex;
1924 uint16_t pipe_qmask;
1926 if (grinder->pcache_r < grinder->pcache_w) {
1927 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
1928 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
1929 grinder->pcache_r++;
1931 uint64_t bmp_slab = 0;
1932 uint32_t bmp_pos = 0;
1934 /* Get another non-empty pipe group */
1935 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
1938 #ifdef RTE_SCHED_DEBUG
1939 debug_check_queue_slab(port, bmp_pos, bmp_slab);
1942 /* Return if pipe group already in one of the other grinders */
1943 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
1944 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
1947 port->grinder_base_bmp_pos[pos] = bmp_pos;
1949 /* Install new pipe group into grinder's pipe cache */
1950 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
1952 pipe_qmask = grinder->pcache_qmask[0];
1953 pipe_qindex = grinder->pcache_qindex[0];
1954 grinder->pcache_r = 1;
1957 /* Install new pipe in the grinder */
1958 grinder->pindex = pipe_qindex >> 4;
1959 grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
1960 grinder->pipe = port->pipe + grinder->pindex;
1961 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
1962 grinder->productive = 0;
1964 grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
1965 grinder_next_tc(port, pos);
1967 /* Check for pipe exhaustion */
1968 if (grinder->pindex == port->pipe_loop) {
1969 port->pipe_exhaustion = 1;
1970 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1978 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
1980 struct rte_sched_grinder *grinder = port->grinder + pos;
1981 struct rte_sched_pipe *pipe = grinder->pipe;
1982 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
1983 uint32_t tc_index = grinder->tc_index;
1984 uint32_t qmask = grinder->qmask;
1987 qindex = tc_index * 4;
1989 grinder->wrr_tokens[0] = ((uint16_t) pipe->wrr_tokens[qindex]) << RTE_SCHED_WRR_SHIFT;
1990 grinder->wrr_tokens[1] = ((uint16_t) pipe->wrr_tokens[qindex + 1]) << RTE_SCHED_WRR_SHIFT;
1991 grinder->wrr_tokens[2] = ((uint16_t) pipe->wrr_tokens[qindex + 2]) << RTE_SCHED_WRR_SHIFT;
1992 grinder->wrr_tokens[3] = ((uint16_t) pipe->wrr_tokens[qindex + 3]) << RTE_SCHED_WRR_SHIFT;
1994 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
1995 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
1996 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
1997 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
1999 grinder->wrr_cost[0] = pipe_params->wrr_cost[qindex];
2000 grinder->wrr_cost[1] = pipe_params->wrr_cost[qindex + 1];
2001 grinder->wrr_cost[2] = pipe_params->wrr_cost[qindex + 2];
2002 grinder->wrr_cost[3] = pipe_params->wrr_cost[qindex + 3];
2006 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
2008 struct rte_sched_grinder *grinder = port->grinder + pos;
2009 struct rte_sched_pipe *pipe = grinder->pipe;
2010 uint32_t tc_index = grinder->tc_index;
2013 qindex = tc_index * 4;
2015 pipe->wrr_tokens[qindex] = (grinder->wrr_tokens[0] & grinder->wrr_mask[0])
2016 >> RTE_SCHED_WRR_SHIFT;
2017 pipe->wrr_tokens[qindex + 1] = (grinder->wrr_tokens[1] & grinder->wrr_mask[1])
2018 >> RTE_SCHED_WRR_SHIFT;
2019 pipe->wrr_tokens[qindex + 2] = (grinder->wrr_tokens[2] & grinder->wrr_mask[2])
2020 >> RTE_SCHED_WRR_SHIFT;
2021 pipe->wrr_tokens[qindex + 3] = (grinder->wrr_tokens[3] & grinder->wrr_mask[3])
2022 >> RTE_SCHED_WRR_SHIFT;
2026 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
2028 struct rte_sched_grinder *grinder = port->grinder + pos;
2029 uint16_t wrr_tokens_min;
2031 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2032 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2033 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2034 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2036 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2037 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2039 grinder->wrr_tokens[0] -= wrr_tokens_min;
2040 grinder->wrr_tokens[1] -= wrr_tokens_min;
2041 grinder->wrr_tokens[2] -= wrr_tokens_min;
2042 grinder->wrr_tokens[3] -= wrr_tokens_min;
2046 #define grinder_evict(port, pos)
2049 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
2051 struct rte_sched_grinder *grinder = port->grinder + pos;
2053 rte_prefetch0(grinder->pipe);
2054 rte_prefetch0(grinder->queue[0]);
2058 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
2060 struct rte_sched_grinder *grinder = port->grinder + pos;
2061 uint16_t qsize, qr[4];
2063 qsize = grinder->qsize;
2064 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2065 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2066 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2067 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2069 rte_prefetch0(grinder->qbase[0] + qr[0]);
2070 rte_prefetch0(grinder->qbase[1] + qr[1]);
2072 grinder_wrr_load(port, pos);
2073 grinder_wrr(port, pos);
2075 rte_prefetch0(grinder->qbase[2] + qr[2]);
2076 rte_prefetch0(grinder->qbase[3] + qr[3]);
2080 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2082 struct rte_sched_grinder *grinder = port->grinder + pos;
2083 uint32_t qpos = grinder->qpos;
2084 struct rte_mbuf **qbase = grinder->qbase[qpos];
2085 uint16_t qsize = grinder->qsize;
2086 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2088 grinder->pkt = qbase[qr];
2089 rte_prefetch0(grinder->pkt);
2091 if (unlikely((qr & 0x7) == 7)) {
2092 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2094 rte_prefetch0(qbase + qr_next);
2098 static inline uint32_t
2099 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2101 struct rte_sched_grinder *grinder = port->grinder + pos;
2103 switch (grinder->state) {
2104 case e_GRINDER_PREFETCH_PIPE:
2106 if (grinder_next_pipe(port, pos)) {
2107 grinder_prefetch_pipe(port, pos);
2108 port->busy_grinders++;
2110 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2117 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2119 struct rte_sched_pipe *pipe = grinder->pipe;
2121 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2122 grinder_prefetch_tc_queue_arrays(port, pos);
2123 grinder_credits_update(port, pos);
2125 grinder->state = e_GRINDER_PREFETCH_MBUF;
2129 case e_GRINDER_PREFETCH_MBUF:
2131 grinder_prefetch_mbuf(port, pos);
2133 grinder->state = e_GRINDER_READ_MBUF;
2137 case e_GRINDER_READ_MBUF:
2139 uint32_t result = 0;
2141 result = grinder_schedule(port, pos);
2143 /* Look for next packet within the same TC */
2144 if (result && grinder->qmask) {
2145 grinder_wrr(port, pos);
2146 grinder_prefetch_mbuf(port, pos);
2150 grinder_wrr_store(port, pos);
2152 /* Look for another active TC within same pipe */
2153 if (grinder_next_tc(port, pos)) {
2154 grinder_prefetch_tc_queue_arrays(port, pos);
2156 grinder->state = e_GRINDER_PREFETCH_MBUF;
2160 if (grinder->productive == 0 &&
2161 port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2162 port->pipe_loop = grinder->pindex;
2164 grinder_evict(port, pos);
2166 /* Look for another active pipe */
2167 if (grinder_next_pipe(port, pos)) {
2168 grinder_prefetch_pipe(port, pos);
2170 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2174 /* No active pipe found */
2175 port->busy_grinders--;
2177 grinder->state = e_GRINDER_PREFETCH_PIPE;
2182 rte_panic("Algorithmic error (invalid state)\n");
2188 rte_sched_port_time_resync(struct rte_sched_port *port)
2190 uint64_t cycles = rte_get_tsc_cycles();
2191 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2192 uint64_t bytes_diff;
2194 /* Compute elapsed time in bytes */
2195 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2196 port->inv_cycles_per_byte);
2198 /* Advance port time */
2199 port->time_cpu_cycles = cycles;
2200 port->time_cpu_bytes += bytes_diff;
2201 if (port->time < port->time_cpu_bytes)
2202 port->time = port->time_cpu_bytes;
2204 /* Reset pipe loop detection */
2205 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2209 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2213 /* Check if any exception flag is set */
2214 exceptions = (second_pass && port->busy_grinders == 0) ||
2215 (port->pipe_exhaustion == 1);
2217 /* Clear exception flags */
2218 port->pipe_exhaustion = 0;
2224 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2228 port->pkts_out = pkts;
2229 port->n_pkts_out = 0;
2231 rte_sched_port_time_resync(port);
2233 /* Take each queue in the grinder one step further */
2234 for (i = 0, count = 0; ; i++) {
2235 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2236 if ((count == n_pkts) ||
2237 rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {