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_MAX_QUEUES_PER_TC RTE_SCHED_BE_QUEUES_PER_PIPE
41 #define RTE_SCHED_GRINDER_PCACHE_SIZE (64 / RTE_SCHED_QUEUES_PER_PIPE)
42 #define RTE_SCHED_PIPE_INVALID UINT32_MAX
43 #define RTE_SCHED_BMP_POS_INVALID UINT32_MAX
45 /* Scaling for cycles_per_byte calculation
46 * Chosen so that minimum rate is 480 bit/sec
48 #define RTE_SCHED_TIME_SHIFT 8
50 struct rte_sched_subport {
51 /* Token bucket (TB) */
52 uint64_t tb_time; /* time of last update */
54 uint32_t tb_credits_per_period;
58 /* Traffic classes (TCs) */
59 uint64_t tc_time; /* time of next update */
60 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
61 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
64 /* TC oversubscription */
66 uint32_t tc_ov_wm_min;
67 uint32_t tc_ov_wm_max;
68 uint8_t tc_ov_period_id;
74 struct rte_sched_subport_stats stats;
77 struct rte_sched_pipe_profile {
78 /* Token bucket (TB) */
80 uint32_t tb_credits_per_period;
83 /* Pipe traffic classes */
85 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
88 /* Pipe best-effort traffic class queues */
89 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
92 struct rte_sched_pipe {
93 /* Token bucket (TB) */
94 uint64_t tb_time; /* time of last update */
97 /* Pipe profile and flags */
100 /* Traffic classes (TCs) */
101 uint64_t tc_time; /* time of next update */
102 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
104 /* Weighted Round Robin (WRR) */
105 uint8_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
107 /* TC oversubscription */
108 uint32_t tc_ov_credits;
109 uint8_t tc_ov_period_id;
111 } __rte_cache_aligned;
113 struct rte_sched_queue {
118 struct rte_sched_queue_extra {
119 struct rte_sched_queue_stats stats;
126 e_GRINDER_PREFETCH_PIPE = 0,
127 e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
128 e_GRINDER_PREFETCH_MBUF,
132 struct rte_sched_grinder {
134 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
135 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
140 enum grinder_state state;
143 struct rte_sched_subport *subport;
144 struct rte_sched_pipe *pipe;
145 struct rte_sched_pipe_profile *pipe_params;
148 uint8_t tccache_qmask[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
149 uint32_t tccache_qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
155 struct rte_sched_queue *queue[RTE_SCHED_MAX_QUEUES_PER_TC];
156 struct rte_mbuf **qbase[RTE_SCHED_MAX_QUEUES_PER_TC];
157 uint32_t qindex[RTE_SCHED_MAX_QUEUES_PER_TC];
161 struct rte_mbuf *pkt;
164 uint16_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
165 uint16_t wrr_mask[RTE_SCHED_BE_QUEUES_PER_PIPE];
166 uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
169 struct rte_sched_port {
170 /* User parameters */
171 uint32_t n_subports_per_port;
172 uint32_t n_pipes_per_subport;
173 uint32_t n_pipes_per_subport_log2;
174 uint16_t pipe_queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
175 uint8_t pipe_tc[RTE_SCHED_QUEUES_PER_PIPE];
176 uint8_t tc_queue[RTE_SCHED_QUEUES_PER_PIPE];
179 uint32_t frame_overhead;
180 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
181 uint32_t n_pipe_profiles;
182 uint32_t pipe_tc3_rate_max;
184 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
188 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cyles */
189 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
190 uint64_t time; /* Current NIC TX time measured in bytes */
191 struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
193 /* Scheduling loop detection */
195 uint32_t pipe_exhaustion;
198 struct rte_bitmap *bmp;
199 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
202 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
203 uint32_t busy_grinders;
204 struct rte_mbuf **pkts_out;
207 /* Queue base calculation */
208 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
211 /* Large data structures */
212 struct rte_sched_subport *subport;
213 struct rte_sched_pipe *pipe;
214 struct rte_sched_queue *queue;
215 struct rte_sched_queue_extra *queue_extra;
216 struct rte_sched_pipe_profile *pipe_profiles;
218 struct rte_mbuf **queue_array;
219 uint8_t memory[0] __rte_cache_aligned;
220 } __rte_cache_aligned;
222 enum rte_sched_port_array {
223 e_RTE_SCHED_PORT_ARRAY_SUBPORT = 0,
224 e_RTE_SCHED_PORT_ARRAY_PIPE,
225 e_RTE_SCHED_PORT_ARRAY_QUEUE,
226 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA,
227 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES,
228 e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY,
229 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY,
230 e_RTE_SCHED_PORT_ARRAY_TOTAL,
233 #ifdef RTE_SCHED_COLLECT_STATS
235 static inline uint32_t
236 rte_sched_port_queues_per_subport(struct rte_sched_port *port)
238 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport;
243 static inline uint32_t
244 rte_sched_port_queues_per_port(struct rte_sched_port *port)
246 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport * port->n_subports_per_port;
249 static inline struct rte_mbuf **
250 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
252 uint32_t pindex = qindex >> 4;
253 uint32_t qpos = qindex & 0xF;
255 return (port->queue_array + pindex *
256 port->qsize_sum + port->qsize_add[qpos]);
259 static inline uint16_t
260 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
262 uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
264 return port->qsize[tc];
267 static inline uint16_t
268 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
270 uint16_t pipe_queue = port->pipe_queue[traffic_class];
275 static inline uint8_t
276 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
278 uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
283 static inline uint8_t
284 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
286 uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
292 pipe_profile_check(struct rte_sched_pipe_params *params,
293 uint32_t rate, uint16_t *qsize)
297 /* Pipe parameters */
301 /* TB rate: non-zero, not greater than port rate */
302 if (params->tb_rate == 0 ||
303 params->tb_rate > rate)
306 /* TB size: non-zero */
307 if (params->tb_size == 0)
310 /* TC rate: non-zero if qsize non-zero, less than pipe rate */
311 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
312 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
313 (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
314 params->tc_rate[i] > params->tb_rate)))
317 if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
318 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0)
321 /* TC period: non-zero */
322 if (params->tc_period == 0)
325 /* TC3 oversubscription weight: non-zero */
326 if (params->tc_ov_weight == 0)
329 /* Queue WRR weights: non-zero */
330 for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
331 if (params->wrr_weights[i] == 0)
339 rte_sched_port_check_params(struct rte_sched_port_params *params)
347 if (params->socket < 0)
351 if (params->rate == 0)
355 if (params->mtu == 0)
358 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
359 if (params->n_subports_per_port == 0 ||
360 params->n_subports_per_port > 1u << 16 ||
361 !rte_is_power_of_2(params->n_subports_per_port))
364 /* n_pipes_per_subport: non-zero, power of 2 */
365 if (params->n_pipes_per_subport == 0 ||
366 !rte_is_power_of_2(params->n_pipes_per_subport))
369 /* qsize: non-zero, power of 2,
370 * no bigger than 32K (due to 16-bit read/write pointers)
372 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
373 uint16_t qsize = params->qsize[i];
375 if ((qsize != 0 && !rte_is_power_of_2(qsize)) ||
376 ((i == RTE_SCHED_TRAFFIC_CLASS_BE) && (qsize == 0)))
380 /* pipe_profiles and n_pipe_profiles */
381 if (params->pipe_profiles == NULL ||
382 params->n_pipe_profiles == 0 ||
383 params->n_pipe_profiles > RTE_SCHED_PIPE_PROFILES_PER_PORT)
386 for (i = 0; i < params->n_pipe_profiles; i++) {
387 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
390 status = pipe_profile_check(p, params->rate, ¶ms->qsize[0]);
399 rte_sched_port_get_array_base(struct rte_sched_port_params *params, enum rte_sched_port_array array)
401 uint32_t n_subports_per_port = params->n_subports_per_port;
402 uint32_t n_pipes_per_subport = params->n_pipes_per_subport;
403 uint32_t n_pipes_per_port = n_pipes_per_subport * n_subports_per_port;
404 uint32_t n_queues_per_port = RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport * n_subports_per_port;
406 uint32_t size_subport = n_subports_per_port * sizeof(struct rte_sched_subport);
407 uint32_t size_pipe = n_pipes_per_port * sizeof(struct rte_sched_pipe);
408 uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue);
409 uint32_t size_queue_extra
410 = n_queues_per_port * sizeof(struct rte_sched_queue_extra);
411 uint32_t size_pipe_profiles
412 = RTE_SCHED_PIPE_PROFILES_PER_PORT * sizeof(struct rte_sched_pipe_profile);
413 uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port);
414 uint32_t size_per_pipe_queue_array, size_queue_array;
418 size_per_pipe_queue_array = 0;
419 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
420 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
421 size_per_pipe_queue_array +=
422 params->qsize[i] * sizeof(struct rte_mbuf *);
424 size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
425 params->qsize[i] * sizeof(struct rte_mbuf *);
427 size_queue_array = n_pipes_per_port * size_per_pipe_queue_array;
431 if (array == e_RTE_SCHED_PORT_ARRAY_SUBPORT)
433 base += RTE_CACHE_LINE_ROUNDUP(size_subport);
435 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE)
437 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
439 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE)
441 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
443 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA)
445 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
447 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES)
449 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
451 if (array == e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY)
453 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
455 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY)
457 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
463 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
465 uint32_t size0, size1;
468 status = rte_sched_port_check_params(params);
470 RTE_LOG(NOTICE, SCHED,
471 "Port scheduler params check failed (%d)\n", status);
476 size0 = sizeof(struct rte_sched_port);
477 size1 = rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_TOTAL);
479 return size0 + size1;
483 rte_sched_port_config_qsize(struct rte_sched_port *port)
487 port->qsize_add[0] = 0;
489 /* Strict prority traffic class */
490 for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
491 port->qsize_add[i] = port->qsize_add[i-1] + port->qsize[i-1];
493 /* Best-effort traffic class */
494 port->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
495 port->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
496 port->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
497 port->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
498 port->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
499 port->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
500 port->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
501 port->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
502 port->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
504 port->qsize_sum = port->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
505 port->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
509 rte_sched_port_log_pipe_profile(struct rte_sched_port *port, uint32_t i)
511 struct rte_sched_pipe_profile *p = port->pipe_profiles + i;
513 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
514 " Token bucket: period = %u, credits per period = %u, size = %u\n"
515 " Traffic classes: period = %u,\n"
516 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
517 " Best-effort traffic class oversubscription: weight = %hhu\n"
518 " WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
523 p->tb_credits_per_period,
526 /* Traffic classes */
528 p->tc_credits_per_period[0],
529 p->tc_credits_per_period[1],
530 p->tc_credits_per_period[2],
531 p->tc_credits_per_period[3],
532 p->tc_credits_per_period[4],
533 p->tc_credits_per_period[5],
534 p->tc_credits_per_period[6],
535 p->tc_credits_per_period[7],
536 p->tc_credits_per_period[8],
537 p->tc_credits_per_period[9],
538 p->tc_credits_per_period[10],
539 p->tc_credits_per_period[11],
540 p->tc_credits_per_period[12],
542 /* Traffic class 3 oversubscription */
546 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
549 static inline uint64_t
550 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
552 uint64_t time = time_ms;
554 time = (time * rate) / 1000;
560 rte_sched_pipe_profile_convert(struct rte_sched_port *port,
561 struct rte_sched_pipe_params *src,
562 struct rte_sched_pipe_profile *dst,
565 uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
566 uint32_t lcd1, lcd2, lcd;
570 if (src->tb_rate == rate) {
571 dst->tb_credits_per_period = 1;
574 double tb_rate = (double) src->tb_rate
576 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
578 rte_approx(tb_rate, d,
579 &dst->tb_credits_per_period, &dst->tb_period);
582 dst->tb_size = src->tb_size;
584 /* Traffic Classes */
585 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
588 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
590 dst->tc_credits_per_period[i]
591 = rte_sched_time_ms_to_bytes(src->tc_period,
594 dst->tc_ov_weight = src->tc_ov_weight;
597 wrr_cost[0] = src->wrr_weights[0];
598 wrr_cost[1] = src->wrr_weights[1];
599 wrr_cost[2] = src->wrr_weights[2];
600 wrr_cost[3] = src->wrr_weights[3];
602 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
603 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
604 lcd = rte_get_lcd(lcd1, lcd2);
606 wrr_cost[0] = lcd / wrr_cost[0];
607 wrr_cost[1] = lcd / wrr_cost[1];
608 wrr_cost[2] = lcd / wrr_cost[2];
609 wrr_cost[3] = lcd / wrr_cost[3];
611 dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
612 dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
613 dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
614 dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
618 rte_sched_port_config_pipe_profile_table(struct rte_sched_port *port,
619 struct rte_sched_port_params *params)
623 for (i = 0; i < port->n_pipe_profiles; i++) {
624 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
625 struct rte_sched_pipe_profile *dst = port->pipe_profiles + i;
627 rte_sched_pipe_profile_convert(port, src, dst, params->rate);
628 rte_sched_port_log_pipe_profile(port, i);
631 port->pipe_tc3_rate_max = 0;
632 for (i = 0; i < port->n_pipe_profiles; i++) {
633 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
634 uint32_t pipe_tc3_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
636 if (port->pipe_tc3_rate_max < pipe_tc3_rate)
637 port->pipe_tc3_rate_max = pipe_tc3_rate;
641 struct rte_sched_port *
642 rte_sched_port_config(struct rte_sched_port_params *params)
644 struct rte_sched_port *port = NULL;
645 uint32_t mem_size, bmp_mem_size, n_queues_per_port, i, j, cycles_per_byte;
647 /* Check user parameters. Determine the amount of memory to allocate */
648 mem_size = rte_sched_port_get_memory_footprint(params);
652 /* Allocate memory to store the data structures */
653 port = rte_zmalloc_socket("qos_params", mem_size, RTE_CACHE_LINE_SIZE,
658 /* compile time checks */
659 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
660 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS & (RTE_SCHED_PORT_N_GRINDERS - 1));
662 /* User parameters */
663 port->n_subports_per_port = params->n_subports_per_port;
664 port->n_pipes_per_subport = params->n_pipes_per_subport;
665 port->n_pipes_per_subport_log2 =
666 __builtin_ctz(params->n_pipes_per_subport);
668 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
669 port->pipe_queue[i] = i;
671 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
672 port->pipe_tc[i] = j;
674 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
678 for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
679 port->tc_queue[i] = j;
681 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
684 port->rate = params->rate;
685 port->mtu = params->mtu + params->frame_overhead;
686 port->frame_overhead = params->frame_overhead;
687 memcpy(port->qsize, params->qsize, sizeof(params->qsize));
688 port->n_pipe_profiles = params->n_pipe_profiles;
691 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
694 for (j = 0; j < RTE_COLORS; j++) {
695 /* if min/max are both zero, then RED is disabled */
696 if ((params->red_params[i][j].min_th |
697 params->red_params[i][j].max_th) == 0) {
701 if (rte_red_config_init(&port->red_config[i][j],
702 params->red_params[i][j].wq_log2,
703 params->red_params[i][j].min_th,
704 params->red_params[i][j].max_th,
705 params->red_params[i][j].maxp_inv) != 0) {
714 port->time_cpu_cycles = rte_get_tsc_cycles();
715 port->time_cpu_bytes = 0;
718 cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
720 port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
722 /* Scheduling loop detection */
723 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
724 port->pipe_exhaustion = 0;
727 port->busy_grinders = 0;
728 port->pkts_out = NULL;
729 port->n_pkts_out = 0;
731 /* Queue base calculation */
732 rte_sched_port_config_qsize(port);
734 /* Large data structures */
735 port->subport = (struct rte_sched_subport *)
736 (port->memory + rte_sched_port_get_array_base(params,
737 e_RTE_SCHED_PORT_ARRAY_SUBPORT));
738 port->pipe = (struct rte_sched_pipe *)
739 (port->memory + rte_sched_port_get_array_base(params,
740 e_RTE_SCHED_PORT_ARRAY_PIPE));
741 port->queue = (struct rte_sched_queue *)
742 (port->memory + rte_sched_port_get_array_base(params,
743 e_RTE_SCHED_PORT_ARRAY_QUEUE));
744 port->queue_extra = (struct rte_sched_queue_extra *)
745 (port->memory + rte_sched_port_get_array_base(params,
746 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA));
747 port->pipe_profiles = (struct rte_sched_pipe_profile *)
748 (port->memory + rte_sched_port_get_array_base(params,
749 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES));
750 port->bmp_array = port->memory
751 + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY);
752 port->queue_array = (struct rte_mbuf **)
753 (port->memory + rte_sched_port_get_array_base(params,
754 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY));
756 /* Pipe profile table */
757 rte_sched_port_config_pipe_profile_table(port, params);
760 n_queues_per_port = rte_sched_port_queues_per_port(port);
761 bmp_mem_size = rte_bitmap_get_memory_footprint(n_queues_per_port);
762 port->bmp = rte_bitmap_init(n_queues_per_port, port->bmp_array,
764 if (port->bmp == NULL) {
765 RTE_LOG(ERR, SCHED, "Bitmap init error\n");
770 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
771 port->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
778 rte_sched_port_free(struct rte_sched_port *port)
781 uint32_t n_queues_per_port;
783 /* Check user parameters */
787 n_queues_per_port = rte_sched_port_queues_per_port(port);
789 /* Free enqueued mbufs */
790 for (qindex = 0; qindex < n_queues_per_port; qindex++) {
791 struct rte_mbuf **mbufs = rte_sched_port_qbase(port, qindex);
792 uint16_t qsize = rte_sched_port_qsize(port, qindex);
794 struct rte_sched_queue *queue = port->queue + qindex;
795 uint16_t qr = queue->qr & (qsize - 1);
796 uint16_t qw = queue->qw & (qsize - 1);
798 for (; qr != qw; qr = (qr + 1) & (qsize - 1))
799 rte_pktmbuf_free(mbufs[qr]);
803 rte_bitmap_free(port->bmp);
808 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
810 struct rte_sched_subport *s = port->subport + i;
812 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
813 " Token bucket: period = %u, credits per period = %u, size = %u\n"
814 " Traffic classes: period = %u\n"
815 " credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
816 " Best effort traffic class oversubscription: wm min = %u, wm max = %u\n",
821 s->tb_credits_per_period,
824 /* Traffic classes */
826 s->tc_credits_per_period[0],
827 s->tc_credits_per_period[1],
828 s->tc_credits_per_period[2],
829 s->tc_credits_per_period[3],
830 s->tc_credits_per_period[4],
831 s->tc_credits_per_period[5],
832 s->tc_credits_per_period[6],
833 s->tc_credits_per_period[7],
834 s->tc_credits_per_period[8],
835 s->tc_credits_per_period[9],
836 s->tc_credits_per_period[10],
837 s->tc_credits_per_period[11],
838 s->tc_credits_per_period[12],
840 /* Traffic class 3 oversubscription */
846 rte_sched_subport_config(struct rte_sched_port *port,
848 struct rte_sched_subport_params *params)
850 struct rte_sched_subport *s;
853 /* Check user parameters */
855 subport_id >= port->n_subports_per_port ||
859 if (params->tb_rate == 0 || params->tb_rate > port->rate)
862 if (params->tb_size == 0)
865 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
866 uint32_t tc_rate = params->tc_rate[i];
867 uint16_t qsize = port->qsize[i];
869 if ((qsize == 0 && tc_rate != 0) ||
870 (qsize != 0 && tc_rate == 0) ||
871 (tc_rate > params->tb_rate))
875 if (port->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
876 params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0)
879 if (params->tc_period == 0)
882 s = port->subport + subport_id;
884 /* Token Bucket (TB) */
885 if (params->tb_rate == port->rate) {
886 s->tb_credits_per_period = 1;
889 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
890 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
892 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
895 s->tb_size = params->tb_size;
896 s->tb_time = port->time;
897 s->tb_credits = s->tb_size / 2;
899 /* Traffic Classes (TCs) */
900 s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
901 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
903 s->tc_credits_per_period[i]
904 = rte_sched_time_ms_to_bytes(params->tc_period,
908 s->tc_time = port->time + s->tc_period;
909 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
911 s->tc_credits[i] = s->tc_credits_per_period[i];
913 /* TC oversubscription */
914 s->tc_ov_wm_min = port->mtu;
915 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
916 port->pipe_tc3_rate_max);
917 s->tc_ov_wm = s->tc_ov_wm_max;
918 s->tc_ov_period_id = 0;
923 rte_sched_port_log_subport_config(port, subport_id);
929 rte_sched_pipe_config(struct rte_sched_port *port,
932 int32_t pipe_profile)
934 struct rte_sched_subport *s;
935 struct rte_sched_pipe *p;
936 struct rte_sched_pipe_profile *params;
937 uint32_t deactivate, profile, i;
939 /* Check user parameters */
940 profile = (uint32_t) pipe_profile;
941 deactivate = (pipe_profile < 0);
944 subport_id >= port->n_subports_per_port ||
945 pipe_id >= port->n_pipes_per_subport ||
946 (!deactivate && profile >= port->n_pipe_profiles))
950 /* Check that subport configuration is valid */
951 s = port->subport + subport_id;
952 if (s->tb_period == 0)
955 p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
957 /* Handle the case when pipe already has a valid configuration */
959 params = port->pipe_profiles + p->profile;
961 double subport_tc3_rate =
962 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
963 / (double) s->tc_period;
964 double pipe_tc3_rate =
965 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
966 / (double) params->tc_period;
967 uint32_t tc3_ov = s->tc_ov;
969 /* Unplug pipe from its subport */
970 s->tc_ov_n -= params->tc_ov_weight;
971 s->tc_ov_rate -= pipe_tc3_rate;
972 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
974 if (s->tc_ov != tc3_ov) {
975 RTE_LOG(DEBUG, SCHED,
976 "Subport %u TC3 oversubscription is OFF (%.4lf >= %.4lf)\n",
977 subport_id, subport_tc3_rate, s->tc_ov_rate);
981 memset(p, 0, sizeof(struct rte_sched_pipe));
987 /* Apply the new pipe configuration */
988 p->profile = profile;
989 params = port->pipe_profiles + p->profile;
991 /* Token Bucket (TB) */
992 p->tb_time = port->time;
993 p->tb_credits = params->tb_size / 2;
995 /* Traffic Classes (TCs) */
996 p->tc_time = port->time + params->tc_period;
998 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1000 p->tc_credits[i] = params->tc_credits_per_period[i];
1003 /* Subport TC3 oversubscription */
1004 double subport_tc3_rate =
1005 (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1006 / (double) s->tc_period;
1007 double pipe_tc3_rate =
1008 (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1009 / (double) params->tc_period;
1010 uint32_t tc3_ov = s->tc_ov;
1012 s->tc_ov_n += params->tc_ov_weight;
1013 s->tc_ov_rate += pipe_tc3_rate;
1014 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
1016 if (s->tc_ov != tc3_ov) {
1017 RTE_LOG(DEBUG, SCHED,
1018 "Subport %u TC3 oversubscription is ON (%.4lf < %.4lf)\n",
1019 subport_id, subport_tc3_rate, s->tc_ov_rate);
1021 p->tc_ov_period_id = s->tc_ov_period_id;
1022 p->tc_ov_credits = s->tc_ov_wm;
1029 rte_sched_port_pipe_profile_add(struct rte_sched_port *port,
1030 struct rte_sched_pipe_params *params,
1031 uint32_t *pipe_profile_id)
1033 struct rte_sched_pipe_profile *pp;
1041 /* Pipe profiles not exceeds the max limit */
1042 if (port->n_pipe_profiles >= RTE_SCHED_PIPE_PROFILES_PER_PORT)
1046 status = pipe_profile_check(params, port->rate, &port->qsize[0]);
1050 pp = &port->pipe_profiles[port->n_pipe_profiles];
1051 rte_sched_pipe_profile_convert(port, params, pp, port->rate);
1053 /* Pipe profile not exists */
1054 for (i = 0; i < port->n_pipe_profiles; i++)
1055 if (memcmp(port->pipe_profiles + i, pp, sizeof(*pp)) == 0)
1058 /* Pipe profile commit */
1059 *pipe_profile_id = port->n_pipe_profiles;
1060 port->n_pipe_profiles++;
1062 if (port->pipe_tc3_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1063 port->pipe_tc3_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1065 rte_sched_port_log_pipe_profile(port, *pipe_profile_id);
1070 static inline uint32_t
1071 rte_sched_port_qindex(struct rte_sched_port *port,
1074 uint32_t traffic_class,
1077 return ((subport & (port->n_subports_per_port - 1)) <<
1078 (port->n_pipes_per_subport_log2 + 4)) |
1079 ((pipe & (port->n_pipes_per_subport - 1)) << 4) |
1080 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1081 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1085 rte_sched_port_pkt_write(struct rte_sched_port *port,
1086 struct rte_mbuf *pkt,
1087 uint32_t subport, uint32_t pipe,
1088 uint32_t traffic_class,
1089 uint32_t queue, enum rte_color color)
1092 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1094 rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1098 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1099 const struct rte_mbuf *pkt,
1100 uint32_t *subport, uint32_t *pipe,
1101 uint32_t *traffic_class, uint32_t *queue)
1103 uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1105 *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1106 *pipe = (queue_id >> 4) & (port->n_pipes_per_subport - 1);
1107 *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1108 *queue = rte_sched_port_tc_queue(port, queue_id);
1112 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1114 return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1118 rte_sched_subport_read_stats(struct rte_sched_port *port,
1119 uint32_t subport_id,
1120 struct rte_sched_subport_stats *stats,
1123 struct rte_sched_subport *s;
1125 /* Check user parameters */
1126 if (port == NULL || subport_id >= port->n_subports_per_port ||
1127 stats == NULL || tc_ov == NULL)
1130 s = port->subport + subport_id;
1132 /* Copy subport stats and clear */
1133 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1134 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1136 /* Subport TC oversubscription status */
1143 rte_sched_queue_read_stats(struct rte_sched_port *port,
1145 struct rte_sched_queue_stats *stats,
1148 struct rte_sched_queue *q;
1149 struct rte_sched_queue_extra *qe;
1151 /* Check user parameters */
1152 if ((port == NULL) ||
1153 (queue_id >= rte_sched_port_queues_per_port(port)) ||
1158 q = port->queue + queue_id;
1159 qe = port->queue_extra + queue_id;
1161 /* Copy queue stats and clear */
1162 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1163 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1166 *qlen = q->qw - q->qr;
1171 #ifdef RTE_SCHED_DEBUG
1174 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1176 struct rte_sched_queue *queue = port->queue + qindex;
1178 return queue->qr == queue->qw;
1181 #endif /* RTE_SCHED_DEBUG */
1183 #ifdef RTE_SCHED_COLLECT_STATS
1186 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1188 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1189 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1190 uint32_t pkt_len = pkt->pkt_len;
1192 s->stats.n_pkts_tc[tc_index] += 1;
1193 s->stats.n_bytes_tc[tc_index] += pkt_len;
1196 #ifdef RTE_SCHED_RED
1198 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1200 struct rte_mbuf *pkt, uint32_t red)
1203 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1205 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1208 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1209 uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1210 uint32_t pkt_len = pkt->pkt_len;
1212 s->stats.n_pkts_tc_dropped[tc_index] += 1;
1213 s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1214 #ifdef RTE_SCHED_RED
1215 s->stats.n_pkts_red_dropped[tc_index] += red;
1220 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1222 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1223 uint32_t pkt_len = pkt->pkt_len;
1225 qe->stats.n_pkts += 1;
1226 qe->stats.n_bytes += pkt_len;
1229 #ifdef RTE_SCHED_RED
1231 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1233 struct rte_mbuf *pkt, uint32_t red)
1236 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1238 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1241 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1242 uint32_t pkt_len = pkt->pkt_len;
1244 qe->stats.n_pkts_dropped += 1;
1245 qe->stats.n_bytes_dropped += pkt_len;
1246 #ifdef RTE_SCHED_RED
1247 qe->stats.n_pkts_red_dropped += red;
1251 #endif /* RTE_SCHED_COLLECT_STATS */
1253 #ifdef RTE_SCHED_RED
1256 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1258 struct rte_sched_queue_extra *qe;
1259 struct rte_red_config *red_cfg;
1260 struct rte_red *red;
1262 enum rte_color color;
1264 tc_index = rte_sched_port_pipe_tc(port, qindex);
1265 color = rte_sched_port_pkt_read_color(pkt);
1266 red_cfg = &port->red_config[tc_index][color];
1268 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1271 qe = port->queue_extra + qindex;
1274 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1278 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1280 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1281 struct rte_red *red = &qe->red;
1283 rte_red_mark_queue_empty(red, port->time);
1288 #define rte_sched_port_red_drop(port, pkt, qindex, qlen) 0
1290 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1292 #endif /* RTE_SCHED_RED */
1294 #ifdef RTE_SCHED_DEBUG
1297 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1304 rte_panic("Empty slab at position %u\n", bmp_pos);
1307 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1308 if (mask & bmp_slab) {
1309 if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1310 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1317 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1321 #endif /* RTE_SCHED_DEBUG */
1323 static inline uint32_t
1324 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port,
1325 struct rte_mbuf *pkt)
1327 struct rte_sched_queue *q;
1328 #ifdef RTE_SCHED_COLLECT_STATS
1329 struct rte_sched_queue_extra *qe;
1331 uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1333 q = port->queue + qindex;
1335 #ifdef RTE_SCHED_COLLECT_STATS
1336 qe = port->queue_extra + qindex;
1344 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1345 uint32_t qindex, struct rte_mbuf **qbase)
1347 struct rte_sched_queue *q;
1348 struct rte_mbuf **q_qw;
1351 q = port->queue + qindex;
1352 qsize = rte_sched_port_qsize(port, qindex);
1353 q_qw = qbase + (q->qw & (qsize - 1));
1355 rte_prefetch0(q_qw);
1356 rte_bitmap_prefetch0(port->bmp, qindex);
1360 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex,
1361 struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1363 struct rte_sched_queue *q;
1367 q = port->queue + qindex;
1368 qsize = rte_sched_port_qsize(port, qindex);
1369 qlen = q->qw - q->qr;
1371 /* Drop the packet (and update drop stats) when queue is full */
1372 if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) ||
1374 rte_pktmbuf_free(pkt);
1375 #ifdef RTE_SCHED_COLLECT_STATS
1376 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt,
1378 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt,
1384 /* Enqueue packet */
1385 qbase[q->qw & (qsize - 1)] = pkt;
1388 /* Activate queue in the port bitmap */
1389 rte_bitmap_set(port->bmp, qindex);
1392 #ifdef RTE_SCHED_COLLECT_STATS
1393 rte_sched_port_update_subport_stats(port, qindex, pkt);
1394 rte_sched_port_update_queue_stats(port, qindex, pkt);
1402 * The enqueue function implements a 4-level pipeline with each stage
1403 * processing two different packets. The purpose of using a pipeline
1404 * is to hide the latency of prefetching the data structures. The
1405 * naming convention is presented in the diagram below:
1407 * p00 _______ p10 _______ p20 _______ p30 _______
1408 * ----->| |----->| |----->| |----->| |----->
1409 * | 0 | | 1 | | 2 | | 3 |
1410 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1415 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1418 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1419 *pkt30, *pkt31, *pkt_last;
1420 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1421 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1422 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1423 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1429 * Less then 6 input packets available, which is not enough to
1432 if (unlikely(n_pkts < 6)) {
1433 struct rte_mbuf **q_base[5];
1436 /* Prefetch the mbuf structure of each packet */
1437 for (i = 0; i < n_pkts; i++)
1438 rte_prefetch0(pkts[i]);
1440 /* Prefetch the queue structure for each queue */
1441 for (i = 0; i < n_pkts; i++)
1442 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port,
1445 /* Prefetch the write pointer location of each queue */
1446 for (i = 0; i < n_pkts; i++) {
1447 q_base[i] = rte_sched_port_qbase(port, q[i]);
1448 rte_sched_port_enqueue_qwa_prefetch0(port, q[i],
1452 /* Write each packet to its queue */
1453 for (i = 0; i < n_pkts; i++)
1454 result += rte_sched_port_enqueue_qwa(port, q[i],
1455 q_base[i], pkts[i]);
1460 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1463 rte_prefetch0(pkt20);
1464 rte_prefetch0(pkt21);
1468 rte_prefetch0(pkt10);
1469 rte_prefetch0(pkt11);
1471 q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1472 q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1476 rte_prefetch0(pkt00);
1477 rte_prefetch0(pkt01);
1479 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1480 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1482 q20_base = rte_sched_port_qbase(port, q20);
1483 q21_base = rte_sched_port_qbase(port, q21);
1484 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1485 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1487 /* Run the pipeline */
1488 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1489 /* Propagate stage inputs */
1500 q30_base = q20_base;
1501 q31_base = q21_base;
1503 /* Stage 0: Get packets in */
1505 pkt01 = pkts[i + 1];
1506 rte_prefetch0(pkt00);
1507 rte_prefetch0(pkt01);
1509 /* Stage 1: Prefetch queue structure storing queue pointers */
1510 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1511 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1513 /* Stage 2: Prefetch queue write location */
1514 q20_base = rte_sched_port_qbase(port, q20);
1515 q21_base = rte_sched_port_qbase(port, q21);
1516 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1517 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1519 /* Stage 3: Write packet to queue and activate queue */
1520 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1521 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1522 result += r30 + r31;
1526 * Drain the pipeline (exactly 6 packets).
1527 * Handle the last packet in the case
1528 * of an odd number of input packets.
1530 pkt_last = pkts[n_pkts - 1];
1531 rte_prefetch0(pkt_last);
1533 q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1534 q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1536 q10_base = rte_sched_port_qbase(port, q10);
1537 q11_base = rte_sched_port_qbase(port, q11);
1538 rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1539 rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1541 r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1542 r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1543 result += r20 + r21;
1545 q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1547 q00_base = rte_sched_port_qbase(port, q00);
1548 q01_base = rte_sched_port_qbase(port, q01);
1549 rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1550 rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1552 r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1553 r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1554 result += r10 + r11;
1556 q_last_base = rte_sched_port_qbase(port, q_last);
1557 rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1559 r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1560 r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1561 result += r00 + r01;
1564 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1571 #ifndef RTE_SCHED_SUBPORT_TC_OV
1574 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1576 struct rte_sched_grinder *grinder = port->grinder + pos;
1577 struct rte_sched_subport *subport = grinder->subport;
1578 struct rte_sched_pipe *pipe = grinder->pipe;
1579 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1584 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1585 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1586 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1587 subport->tb_time += n_periods * subport->tb_period;
1590 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1591 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1592 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1593 pipe->tb_time += n_periods * params->tb_period;
1596 if (unlikely(port->time >= subport->tc_time)) {
1597 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1598 subport->tc_credits[i] = subport->tc_credits_per_period[i];
1600 subport->tc_time = port->time + subport->tc_period;
1604 if (unlikely(port->time >= pipe->tc_time)) {
1605 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1606 pipe->tc_credits[i] = params->tc_credits_per_period[i];
1608 pipe->tc_time = port->time + params->tc_period;
1614 static inline uint32_t
1615 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1617 struct rte_sched_grinder *grinder = port->grinder + pos;
1618 struct rte_sched_subport *subport = grinder->subport;
1619 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1620 uint32_t tc_consumption = 0, tc_ov_consumption_max;
1621 uint32_t tc_ov_wm = subport->tc_ov_wm;
1624 if (subport->tc_ov == 0)
1625 return subport->tc_ov_wm_max;
1627 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
1628 tc_ov_consumption[i] =
1629 subport->tc_credits_per_period[i] - subport->tc_credits[i];
1630 tc_consumption += tc_ov_consumption[i];
1633 tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
1634 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
1635 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
1637 tc_ov_consumption_max =
1638 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
1641 if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
1642 (tc_ov_consumption_max - port->mtu)) {
1643 tc_ov_wm -= tc_ov_wm >> 7;
1644 if (tc_ov_wm < subport->tc_ov_wm_min)
1645 tc_ov_wm = subport->tc_ov_wm_min;
1650 tc_ov_wm += (tc_ov_wm >> 7) + 1;
1651 if (tc_ov_wm > subport->tc_ov_wm_max)
1652 tc_ov_wm = subport->tc_ov_wm_max;
1658 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1660 struct rte_sched_grinder *grinder = port->grinder + pos;
1661 struct rte_sched_subport *subport = grinder->subport;
1662 struct rte_sched_pipe *pipe = grinder->pipe;
1663 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1668 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1669 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1670 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1671 subport->tb_time += n_periods * subport->tb_period;
1674 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1675 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1676 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1677 pipe->tb_time += n_periods * params->tb_period;
1680 if (unlikely(port->time >= subport->tc_time)) {
1681 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
1683 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1684 subport->tc_credits[i] = subport->tc_credits_per_period[i];
1686 subport->tc_time = port->time + subport->tc_period;
1687 subport->tc_ov_period_id++;
1691 if (unlikely(port->time >= pipe->tc_time)) {
1692 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1693 pipe->tc_credits[i] = params->tc_credits_per_period[i];
1694 pipe->tc_time = port->time + params->tc_period;
1697 /* Pipe TCs - Oversubscription */
1698 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
1699 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
1701 pipe->tc_ov_period_id = subport->tc_ov_period_id;
1705 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
1708 #ifndef RTE_SCHED_SUBPORT_TC_OV
1711 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1713 struct rte_sched_grinder *grinder = port->grinder + pos;
1714 struct rte_sched_subport *subport = grinder->subport;
1715 struct rte_sched_pipe *pipe = grinder->pipe;
1716 struct rte_mbuf *pkt = grinder->pkt;
1717 uint32_t tc_index = grinder->tc_index;
1718 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1719 uint32_t subport_tb_credits = subport->tb_credits;
1720 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1721 uint32_t pipe_tb_credits = pipe->tb_credits;
1722 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1725 /* Check queue credits */
1726 enough_credits = (pkt_len <= subport_tb_credits) &&
1727 (pkt_len <= subport_tc_credits) &&
1728 (pkt_len <= pipe_tb_credits) &&
1729 (pkt_len <= pipe_tc_credits);
1731 if (!enough_credits)
1734 /* Update port credits */
1735 subport->tb_credits -= pkt_len;
1736 subport->tc_credits[tc_index] -= pkt_len;
1737 pipe->tb_credits -= pkt_len;
1738 pipe->tc_credits[tc_index] -= pkt_len;
1746 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1748 struct rte_sched_grinder *grinder = port->grinder + pos;
1749 struct rte_sched_subport *subport = grinder->subport;
1750 struct rte_sched_pipe *pipe = grinder->pipe;
1751 struct rte_mbuf *pkt = grinder->pkt;
1752 uint32_t tc_index = grinder->tc_index;
1753 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1754 uint32_t subport_tb_credits = subport->tb_credits;
1755 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1756 uint32_t pipe_tb_credits = pipe->tb_credits;
1757 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1758 uint32_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1759 uint32_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
1760 uint32_t pipe_tc_ov_credits, i;
1763 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1764 pipe_tc_ov_mask1[i] = UINT32_MAX;
1766 pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
1767 pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = UINT32_MAX;
1768 pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
1770 /* Check pipe and subport credits */
1771 enough_credits = (pkt_len <= subport_tb_credits) &&
1772 (pkt_len <= subport_tc_credits) &&
1773 (pkt_len <= pipe_tb_credits) &&
1774 (pkt_len <= pipe_tc_credits) &&
1775 (pkt_len <= pipe_tc_ov_credits);
1777 if (!enough_credits)
1780 /* Update pipe and subport credits */
1781 subport->tb_credits -= pkt_len;
1782 subport->tc_credits[tc_index] -= pkt_len;
1783 pipe->tb_credits -= pkt_len;
1784 pipe->tc_credits[tc_index] -= pkt_len;
1785 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
1790 #endif /* RTE_SCHED_SUBPORT_TC_OV */
1794 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
1796 struct rte_sched_grinder *grinder = port->grinder + pos;
1797 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
1798 struct rte_mbuf *pkt = grinder->pkt;
1799 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1800 uint32_t be_tc_active;
1802 if (!grinder_credits_check(port, pos))
1805 /* Advance port time */
1806 port->time += pkt_len;
1809 port->pkts_out[port->n_pkts_out++] = pkt;
1812 be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
1813 grinder->wrr_tokens[grinder->qpos] +=
1814 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
1816 if (queue->qr == queue->qw) {
1817 uint32_t qindex = grinder->qindex[grinder->qpos];
1819 rte_bitmap_clear(port->bmp, qindex);
1820 grinder->qmask &= ~(1 << grinder->qpos);
1822 grinder->wrr_mask[grinder->qpos] = 0;
1823 rte_sched_port_set_queue_empty_timestamp(port, qindex);
1826 /* Reset pipe loop detection */
1827 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1828 grinder->productive = 1;
1833 #ifdef SCHED_VECTOR_SSE4
1836 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1838 __m128i index = _mm_set1_epi32(base_pipe);
1839 __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
1840 __m128i res = _mm_cmpeq_epi32(pipes, index);
1842 pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
1843 pipes = _mm_cmpeq_epi32(pipes, index);
1844 res = _mm_or_si128(res, pipes);
1846 if (_mm_testz_si128(res, res))
1852 #elif defined(SCHED_VECTOR_NEON)
1855 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1857 uint32x4_t index, pipes;
1858 uint32_t *pos = (uint32_t *)port->grinder_base_bmp_pos;
1860 index = vmovq_n_u32(base_pipe);
1861 pipes = vld1q_u32(pos);
1862 if (!vminvq_u32(veorq_u32(pipes, index)))
1865 pipes = vld1q_u32(pos + 4);
1866 if (!vminvq_u32(veorq_u32(pipes, index)))
1875 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1879 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
1880 if (port->grinder_base_bmp_pos[i] == base_pipe)
1887 #endif /* RTE_SCHED_OPTIMIZATIONS */
1890 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
1892 struct rte_sched_grinder *grinder = port->grinder + pos;
1895 grinder->pcache_w = 0;
1896 grinder->pcache_r = 0;
1898 w[0] = (uint16_t) bmp_slab;
1899 w[1] = (uint16_t) (bmp_slab >> 16);
1900 w[2] = (uint16_t) (bmp_slab >> 32);
1901 w[3] = (uint16_t) (bmp_slab >> 48);
1903 grinder->pcache_qmask[grinder->pcache_w] = w[0];
1904 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
1905 grinder->pcache_w += (w[0] != 0);
1907 grinder->pcache_qmask[grinder->pcache_w] = w[1];
1908 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
1909 grinder->pcache_w += (w[1] != 0);
1911 grinder->pcache_qmask[grinder->pcache_w] = w[2];
1912 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
1913 grinder->pcache_w += (w[2] != 0);
1915 grinder->pcache_qmask[grinder->pcache_w] = w[3];
1916 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
1917 grinder->pcache_w += (w[3] != 0);
1921 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
1923 struct rte_sched_grinder *grinder = port->grinder + pos;
1926 grinder->tccache_w = 0;
1927 grinder->tccache_r = 0;
1929 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
1930 b = (uint8_t) ((qmask >> i) & 0x1);
1931 grinder->tccache_qmask[grinder->tccache_w] = b;
1932 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
1933 grinder->tccache_w += (b != 0);
1936 b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
1937 grinder->tccache_qmask[grinder->tccache_w] = b;
1938 grinder->tccache_qindex[grinder->tccache_w] = qindex +
1939 RTE_SCHED_TRAFFIC_CLASS_BE;
1940 grinder->tccache_w += (b != 0);
1944 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
1946 struct rte_sched_grinder *grinder = port->grinder + pos;
1947 struct rte_mbuf **qbase;
1951 if (grinder->tccache_r == grinder->tccache_w)
1954 qindex = grinder->tccache_qindex[grinder->tccache_r];
1955 qbase = rte_sched_port_qbase(port, qindex);
1956 qsize = rte_sched_port_qsize(port, qindex);
1958 grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
1959 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
1960 grinder->qsize = qsize;
1962 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
1963 grinder->queue[0] = port->queue + qindex;
1964 grinder->qbase[0] = qbase;
1965 grinder->qindex[0] = qindex;
1966 grinder->tccache_r++;
1971 grinder->queue[0] = port->queue + qindex;
1972 grinder->queue[1] = port->queue + qindex + 1;
1973 grinder->queue[2] = port->queue + qindex + 2;
1974 grinder->queue[3] = port->queue + qindex + 3;
1976 grinder->qbase[0] = qbase;
1977 grinder->qbase[1] = qbase + qsize;
1978 grinder->qbase[2] = qbase + 2 * qsize;
1979 grinder->qbase[3] = qbase + 3 * qsize;
1981 grinder->qindex[0] = qindex;
1982 grinder->qindex[1] = qindex + 1;
1983 grinder->qindex[2] = qindex + 2;
1984 grinder->qindex[3] = qindex + 3;
1986 grinder->tccache_r++;
1991 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
1993 struct rte_sched_grinder *grinder = port->grinder + pos;
1994 uint32_t pipe_qindex;
1995 uint16_t pipe_qmask;
1997 if (grinder->pcache_r < grinder->pcache_w) {
1998 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
1999 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2000 grinder->pcache_r++;
2002 uint64_t bmp_slab = 0;
2003 uint32_t bmp_pos = 0;
2005 /* Get another non-empty pipe group */
2006 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
2009 #ifdef RTE_SCHED_DEBUG
2010 debug_check_queue_slab(port, bmp_pos, bmp_slab);
2013 /* Return if pipe group already in one of the other grinders */
2014 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2015 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
2018 port->grinder_base_bmp_pos[pos] = bmp_pos;
2020 /* Install new pipe group into grinder's pipe cache */
2021 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
2023 pipe_qmask = grinder->pcache_qmask[0];
2024 pipe_qindex = grinder->pcache_qindex[0];
2025 grinder->pcache_r = 1;
2028 /* Install new pipe in the grinder */
2029 grinder->pindex = pipe_qindex >> 4;
2030 grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
2031 grinder->pipe = port->pipe + grinder->pindex;
2032 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2033 grinder->productive = 0;
2035 grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
2036 grinder_next_tc(port, pos);
2038 /* Check for pipe exhaustion */
2039 if (grinder->pindex == port->pipe_loop) {
2040 port->pipe_exhaustion = 1;
2041 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2049 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
2051 struct rte_sched_grinder *grinder = port->grinder + pos;
2052 struct rte_sched_pipe *pipe = grinder->pipe;
2053 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2054 uint32_t qmask = grinder->qmask;
2056 grinder->wrr_tokens[0] =
2057 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2058 grinder->wrr_tokens[1] =
2059 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2060 grinder->wrr_tokens[2] =
2061 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2062 grinder->wrr_tokens[3] =
2063 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2065 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2066 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2067 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2068 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2070 grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2071 grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2072 grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2073 grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2077 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
2079 struct rte_sched_grinder *grinder = port->grinder + pos;
2080 struct rte_sched_pipe *pipe = grinder->pipe;
2082 pipe->wrr_tokens[0] =
2083 (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2084 RTE_SCHED_WRR_SHIFT;
2085 pipe->wrr_tokens[1] =
2086 (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2087 RTE_SCHED_WRR_SHIFT;
2088 pipe->wrr_tokens[2] =
2089 (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2090 RTE_SCHED_WRR_SHIFT;
2091 pipe->wrr_tokens[3] =
2092 (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2093 RTE_SCHED_WRR_SHIFT;
2097 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
2099 struct rte_sched_grinder *grinder = port->grinder + pos;
2100 uint16_t wrr_tokens_min;
2102 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2103 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2104 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2105 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2107 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2108 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2110 grinder->wrr_tokens[0] -= wrr_tokens_min;
2111 grinder->wrr_tokens[1] -= wrr_tokens_min;
2112 grinder->wrr_tokens[2] -= wrr_tokens_min;
2113 grinder->wrr_tokens[3] -= wrr_tokens_min;
2117 #define grinder_evict(port, pos)
2120 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
2122 struct rte_sched_grinder *grinder = port->grinder + pos;
2124 rte_prefetch0(grinder->pipe);
2125 rte_prefetch0(grinder->queue[0]);
2129 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
2131 struct rte_sched_grinder *grinder = port->grinder + pos;
2132 uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2134 qsize = grinder->qsize;
2137 if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2138 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2140 rte_prefetch0(grinder->qbase[0] + qr[0]);
2144 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2145 qr[1] = grinder->queue[1]->qr & (qsize - 1);
2146 qr[2] = grinder->queue[2]->qr & (qsize - 1);
2147 qr[3] = grinder->queue[3]->qr & (qsize - 1);
2149 rte_prefetch0(grinder->qbase[0] + qr[0]);
2150 rte_prefetch0(grinder->qbase[1] + qr[1]);
2152 grinder_wrr_load(port, pos);
2153 grinder_wrr(port, pos);
2155 rte_prefetch0(grinder->qbase[2] + qr[2]);
2156 rte_prefetch0(grinder->qbase[3] + qr[3]);
2160 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2162 struct rte_sched_grinder *grinder = port->grinder + pos;
2163 uint32_t qpos = grinder->qpos;
2164 struct rte_mbuf **qbase = grinder->qbase[qpos];
2165 uint16_t qsize = grinder->qsize;
2166 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2168 grinder->pkt = qbase[qr];
2169 rte_prefetch0(grinder->pkt);
2171 if (unlikely((qr & 0x7) == 7)) {
2172 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2174 rte_prefetch0(qbase + qr_next);
2178 static inline uint32_t
2179 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2181 struct rte_sched_grinder *grinder = port->grinder + pos;
2183 switch (grinder->state) {
2184 case e_GRINDER_PREFETCH_PIPE:
2186 if (grinder_next_pipe(port, pos)) {
2187 grinder_prefetch_pipe(port, pos);
2188 port->busy_grinders++;
2190 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2197 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2199 struct rte_sched_pipe *pipe = grinder->pipe;
2201 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2202 grinder_prefetch_tc_queue_arrays(port, pos);
2203 grinder_credits_update(port, pos);
2205 grinder->state = e_GRINDER_PREFETCH_MBUF;
2209 case e_GRINDER_PREFETCH_MBUF:
2211 grinder_prefetch_mbuf(port, pos);
2213 grinder->state = e_GRINDER_READ_MBUF;
2217 case e_GRINDER_READ_MBUF:
2219 uint32_t wrr_active, result = 0;
2221 result = grinder_schedule(port, pos);
2223 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2225 /* Look for next packet within the same TC */
2226 if (result && grinder->qmask) {
2228 grinder_wrr(port, pos);
2230 grinder_prefetch_mbuf(port, pos);
2236 grinder_wrr_store(port, pos);
2238 /* Look for another active TC within same pipe */
2239 if (grinder_next_tc(port, pos)) {
2240 grinder_prefetch_tc_queue_arrays(port, pos);
2242 grinder->state = e_GRINDER_PREFETCH_MBUF;
2246 if (grinder->productive == 0 &&
2247 port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2248 port->pipe_loop = grinder->pindex;
2250 grinder_evict(port, pos);
2252 /* Look for another active pipe */
2253 if (grinder_next_pipe(port, pos)) {
2254 grinder_prefetch_pipe(port, pos);
2256 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2260 /* No active pipe found */
2261 port->busy_grinders--;
2263 grinder->state = e_GRINDER_PREFETCH_PIPE;
2268 rte_panic("Algorithmic error (invalid state)\n");
2274 rte_sched_port_time_resync(struct rte_sched_port *port)
2276 uint64_t cycles = rte_get_tsc_cycles();
2277 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2278 uint64_t bytes_diff;
2280 /* Compute elapsed time in bytes */
2281 bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2282 port->inv_cycles_per_byte);
2284 /* Advance port time */
2285 port->time_cpu_cycles = cycles;
2286 port->time_cpu_bytes += bytes_diff;
2287 if (port->time < port->time_cpu_bytes)
2288 port->time = port->time_cpu_bytes;
2290 /* Reset pipe loop detection */
2291 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2295 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2299 /* Check if any exception flag is set */
2300 exceptions = (second_pass && port->busy_grinders == 0) ||
2301 (port->pipe_exhaustion == 1);
2303 /* Clear exception flags */
2304 port->pipe_exhaustion = 0;
2310 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2314 port->pkts_out = pkts;
2315 port->n_pkts_out = 0;
2317 rte_sched_port_time_resync(port);
2319 /* Take each queue in the grinder one step further */
2320 for (i = 0, count = 0; ; i++) {
2321 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2322 if ((count == n_pkts) ||
2323 rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {