4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_common.h>
39 #include <rte_memory.h>
40 #include <rte_malloc.h>
41 #include <rte_cycles.h>
42 #include <rte_prefetch.h>
43 #include <rte_branch_prediction.h>
46 #include "rte_sched.h"
47 #include "rte_bitmap.h"
48 #include "rte_sched_common.h"
49 #include "rte_approx.h"
51 #ifdef __INTEL_COMPILER
52 #pragma warning(disable:2259) /* conversion may lose significant bits */
55 #ifdef RTE_SCHED_VECTOR
56 #include <immintrin.h>
59 #define RTE_SCHED_TB_RATE_CONFIG_ERR (1e-7)
60 #define RTE_SCHED_WRR_SHIFT 3
61 #define RTE_SCHED_GRINDER_PCACHE_SIZE (64 / RTE_SCHED_QUEUES_PER_PIPE)
62 #define RTE_SCHED_PIPE_INVALID UINT32_MAX
63 #define RTE_SCHED_BMP_POS_INVALID UINT32_MAX
65 struct rte_sched_subport {
66 /* Token bucket (TB) */
67 uint64_t tb_time; /* time of last update */
69 uint32_t tb_credits_per_period;
73 /* Traffic classes (TCs) */
74 uint64_t tc_time; /* time of next update */
75 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
76 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
79 /* TC oversubscription */
81 uint32_t tc_ov_wm_min;
82 uint32_t tc_ov_wm_max;
83 uint8_t tc_ov_period_id;
89 struct rte_sched_subport_stats stats;
92 struct rte_sched_pipe_profile {
93 /* Token bucket (TB) */
95 uint32_t tb_credits_per_period;
98 /* Pipe traffic classes */
100 uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
101 uint8_t tc_ov_weight;
104 uint8_t wrr_cost[RTE_SCHED_QUEUES_PER_PIPE];
107 struct rte_sched_pipe {
108 /* Token bucket (TB) */
109 uint64_t tb_time; /* time of last update */
112 /* Pipe profile and flags */
115 /* Traffic classes (TCs) */
116 uint64_t tc_time; /* time of next update */
117 uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
119 /* Weighted Round Robin (WRR) */
120 uint8_t wrr_tokens[RTE_SCHED_QUEUES_PER_PIPE];
122 /* TC oversubscription */
123 uint32_t tc_ov_credits;
124 uint8_t tc_ov_period_id;
126 } __rte_cache_aligned;
128 struct rte_sched_queue {
133 struct rte_sched_queue_extra {
134 struct rte_sched_queue_stats stats;
141 e_GRINDER_PREFETCH_PIPE = 0,
142 e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
143 e_GRINDER_PREFETCH_MBUF,
148 * Path through the scheduler hierarchy used by the scheduler enqueue
149 * operation to identify the destination queue for the current
150 * packet. Stored in the field pkt.hash.sched of struct rte_mbuf of
151 * each packet, typically written by the classification stage and read
152 * by scheduler enqueue.
154 struct rte_sched_port_hierarchy {
155 uint16_t queue:2; /**< Queue ID (0 .. 3) */
156 uint16_t traffic_class:2; /**< Traffic class ID (0 .. 3)*/
157 uint32_t color:2; /**< Color */
159 uint16_t subport; /**< Subport ID */
160 uint32_t pipe; /**< Pipe ID */
163 struct rte_sched_grinder {
165 uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
166 uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
171 enum grinder_state state;
174 struct rte_sched_subport *subport;
175 struct rte_sched_pipe *pipe;
176 struct rte_sched_pipe_profile *pipe_params;
179 uint8_t tccache_qmask[4];
180 uint32_t tccache_qindex[4];
186 struct rte_sched_queue *queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
187 struct rte_mbuf **qbase[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
188 uint32_t qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
192 struct rte_mbuf *pkt;
195 uint16_t wrr_tokens[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
196 uint16_t wrr_mask[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
197 uint8_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
200 struct rte_sched_port {
201 /* User parameters */
202 uint32_t n_subports_per_port;
203 uint32_t n_pipes_per_subport;
206 uint32_t frame_overhead;
207 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
208 uint32_t n_pipe_profiles;
209 uint32_t pipe_tc3_rate_max;
211 struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][e_RTE_METER_COLORS];
215 uint64_t time_cpu_cycles; /* Current CPU time measured in CPU cyles */
216 uint64_t time_cpu_bytes; /* Current CPU time measured in bytes */
217 uint64_t time; /* Current NIC TX time measured in bytes */
218 double cycles_per_byte; /* CPU cycles per byte */
220 /* Scheduling loop detection */
222 uint32_t pipe_exhaustion;
225 struct rte_bitmap *bmp;
226 uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
229 struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
230 uint32_t busy_grinders;
231 struct rte_mbuf **pkts_out;
234 /* Queue base calculation */
235 uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
238 /* Large data structures */
239 struct rte_sched_subport *subport;
240 struct rte_sched_pipe *pipe;
241 struct rte_sched_queue *queue;
242 struct rte_sched_queue_extra *queue_extra;
243 struct rte_sched_pipe_profile *pipe_profiles;
245 struct rte_mbuf **queue_array;
246 uint8_t memory[0] __rte_cache_aligned;
247 } __rte_cache_aligned;
249 enum rte_sched_port_array {
250 e_RTE_SCHED_PORT_ARRAY_SUBPORT = 0,
251 e_RTE_SCHED_PORT_ARRAY_PIPE,
252 e_RTE_SCHED_PORT_ARRAY_QUEUE,
253 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA,
254 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES,
255 e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY,
256 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY,
257 e_RTE_SCHED_PORT_ARRAY_TOTAL,
260 #ifdef RTE_SCHED_COLLECT_STATS
262 static inline uint32_t
263 rte_sched_port_queues_per_subport(struct rte_sched_port *port)
265 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport;
270 static inline uint32_t
271 rte_sched_port_queues_per_port(struct rte_sched_port *port)
273 return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport * port->n_subports_per_port;
276 static inline struct rte_mbuf **
277 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
279 uint32_t pindex = qindex >> 4;
280 uint32_t qpos = qindex & 0xF;
282 return (port->queue_array + pindex *
283 port->qsize_sum + port->qsize_add[qpos]);
286 static inline uint16_t
287 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
289 uint32_t tc = (qindex >> 2) & 0x3;
291 return port->qsize[tc];
295 rte_sched_port_check_params(struct rte_sched_port_params *params)
303 if ((params->socket < 0) || (params->socket >= RTE_MAX_NUMA_NODES))
307 if (params->rate == 0)
311 if (params->mtu == 0)
314 /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
315 if (params->n_subports_per_port == 0 ||
316 params->n_subports_per_port > 1u << 16 ||
317 !rte_is_power_of_2(params->n_subports_per_port))
320 /* n_pipes_per_subport: non-zero, power of 2 */
321 if (params->n_pipes_per_subport == 0 ||
322 !rte_is_power_of_2(params->n_pipes_per_subport))
325 /* qsize: non-zero, power of 2,
326 * no bigger than 32K (due to 16-bit read/write pointers)
328 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
329 uint16_t qsize = params->qsize[i];
331 if (qsize == 0 || !rte_is_power_of_2(qsize))
335 /* pipe_profiles and n_pipe_profiles */
336 if (params->pipe_profiles == NULL ||
337 params->n_pipe_profiles == 0 ||
338 params->n_pipe_profiles > RTE_SCHED_PIPE_PROFILES_PER_PORT)
341 for (i = 0; i < params->n_pipe_profiles; i++) {
342 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
344 /* TB rate: non-zero, not greater than port rate */
345 if (p->tb_rate == 0 || p->tb_rate > params->rate)
348 /* TB size: non-zero */
352 /* TC rate: non-zero, less than pipe rate */
353 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
354 if (p->tc_rate[j] == 0 || p->tc_rate[j] > p->tb_rate)
358 /* TC period: non-zero */
359 if (p->tc_period == 0)
362 #ifdef RTE_SCHED_SUBPORT_TC_OV
363 /* TC3 oversubscription weight: non-zero */
364 if (p->tc_ov_weight == 0)
368 /* Queue WRR weights: non-zero */
369 for (j = 0; j < RTE_SCHED_QUEUES_PER_PIPE; j++) {
370 if (p->wrr_weights[j] == 0)
379 rte_sched_port_get_array_base(struct rte_sched_port_params *params, enum rte_sched_port_array array)
381 uint32_t n_subports_per_port = params->n_subports_per_port;
382 uint32_t n_pipes_per_subport = params->n_pipes_per_subport;
383 uint32_t n_pipes_per_port = n_pipes_per_subport * n_subports_per_port;
384 uint32_t n_queues_per_port = RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport * n_subports_per_port;
386 uint32_t size_subport = n_subports_per_port * sizeof(struct rte_sched_subport);
387 uint32_t size_pipe = n_pipes_per_port * sizeof(struct rte_sched_pipe);
388 uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue);
389 uint32_t size_queue_extra
390 = n_queues_per_port * sizeof(struct rte_sched_queue_extra);
391 uint32_t size_pipe_profiles
392 = RTE_SCHED_PIPE_PROFILES_PER_PORT * sizeof(struct rte_sched_pipe_profile);
393 uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port);
394 uint32_t size_per_pipe_queue_array, size_queue_array;
398 size_per_pipe_queue_array = 0;
399 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
400 size_per_pipe_queue_array += RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS
401 * params->qsize[i] * sizeof(struct rte_mbuf *);
403 size_queue_array = n_pipes_per_port * size_per_pipe_queue_array;
407 if (array == e_RTE_SCHED_PORT_ARRAY_SUBPORT)
409 base += RTE_CACHE_LINE_ROUNDUP(size_subport);
411 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE)
413 base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
415 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE)
417 base += RTE_CACHE_LINE_ROUNDUP(size_queue);
419 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA)
421 base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
423 if (array == e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES)
425 base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
427 if (array == e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY)
429 base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
431 if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY)
433 base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
439 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
441 uint32_t size0, size1;
444 status = rte_sched_port_check_params(params);
446 RTE_LOG(NOTICE, SCHED,
447 "Port scheduler params check failed (%d)\n", status);
452 size0 = sizeof(struct rte_sched_port);
453 size1 = rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_TOTAL);
455 return (size0 + size1);
459 rte_sched_port_config_qsize(struct rte_sched_port *port)
462 port->qsize_add[0] = 0;
463 port->qsize_add[1] = port->qsize_add[0] + port->qsize[0];
464 port->qsize_add[2] = port->qsize_add[1] + port->qsize[0];
465 port->qsize_add[3] = port->qsize_add[2] + port->qsize[0];
468 port->qsize_add[4] = port->qsize_add[3] + port->qsize[0];
469 port->qsize_add[5] = port->qsize_add[4] + port->qsize[1];
470 port->qsize_add[6] = port->qsize_add[5] + port->qsize[1];
471 port->qsize_add[7] = port->qsize_add[6] + port->qsize[1];
474 port->qsize_add[8] = port->qsize_add[7] + port->qsize[1];
475 port->qsize_add[9] = port->qsize_add[8] + port->qsize[2];
476 port->qsize_add[10] = port->qsize_add[9] + port->qsize[2];
477 port->qsize_add[11] = port->qsize_add[10] + port->qsize[2];
480 port->qsize_add[12] = port->qsize_add[11] + port->qsize[2];
481 port->qsize_add[13] = port->qsize_add[12] + port->qsize[3];
482 port->qsize_add[14] = port->qsize_add[13] + port->qsize[3];
483 port->qsize_add[15] = port->qsize_add[14] + port->qsize[3];
485 port->qsize_sum = port->qsize_add[15] + port->qsize[3];
489 rte_sched_port_log_pipe_profile(struct rte_sched_port *port, uint32_t i)
491 struct rte_sched_pipe_profile *p = port->pipe_profiles + i;
493 RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
494 " Token bucket: period = %u, credits per period = %u, size = %u\n"
495 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
496 " Traffic class 3 oversubscription: weight = %hhu\n"
497 " WRR cost: [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu], [%hhu, %hhu, %hhu, %hhu]\n",
502 p->tb_credits_per_period,
505 /* Traffic classes */
507 p->tc_credits_per_period[0],
508 p->tc_credits_per_period[1],
509 p->tc_credits_per_period[2],
510 p->tc_credits_per_period[3],
512 /* Traffic class 3 oversubscription */
516 p->wrr_cost[ 0], p->wrr_cost[ 1], p->wrr_cost[ 2], p->wrr_cost[ 3],
517 p->wrr_cost[ 4], p->wrr_cost[ 5], p->wrr_cost[ 6], p->wrr_cost[ 7],
518 p->wrr_cost[ 8], p->wrr_cost[ 9], p->wrr_cost[10], p->wrr_cost[11],
519 p->wrr_cost[12], p->wrr_cost[13], p->wrr_cost[14], p->wrr_cost[15]);
522 static inline uint64_t
523 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
525 uint64_t time = time_ms;
527 time = (time * rate) / 1000;
533 rte_sched_port_config_pipe_profile_table(struct rte_sched_port *port, struct rte_sched_port_params *params)
537 for (i = 0; i < port->n_pipe_profiles; i++) {
538 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
539 struct rte_sched_pipe_profile *dst = port->pipe_profiles + i;
542 if (src->tb_rate == params->rate) {
543 dst->tb_credits_per_period = 1;
546 double tb_rate = (double) src->tb_rate
547 / (double) params->rate;
548 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
550 rte_approx(tb_rate, d,
551 &dst->tb_credits_per_period, &dst->tb_period);
553 dst->tb_size = src->tb_size;
555 /* Traffic Classes */
556 dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
559 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++)
560 dst->tc_credits_per_period[j]
561 = rte_sched_time_ms_to_bytes(src->tc_period,
564 #ifdef RTE_SCHED_SUBPORT_TC_OV
565 dst->tc_ov_weight = src->tc_ov_weight;
569 for (j = 0; j < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; j++) {
570 uint32_t wrr_cost[RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS];
571 uint32_t lcd, lcd1, lcd2;
574 qindex = j * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS;
576 wrr_cost[0] = src->wrr_weights[qindex];
577 wrr_cost[1] = src->wrr_weights[qindex + 1];
578 wrr_cost[2] = src->wrr_weights[qindex + 2];
579 wrr_cost[3] = src->wrr_weights[qindex + 3];
581 lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
582 lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
583 lcd = rte_get_lcd(lcd1, lcd2);
585 wrr_cost[0] = lcd / wrr_cost[0];
586 wrr_cost[1] = lcd / wrr_cost[1];
587 wrr_cost[2] = lcd / wrr_cost[2];
588 wrr_cost[3] = lcd / wrr_cost[3];
590 dst->wrr_cost[qindex] = (uint8_t) wrr_cost[0];
591 dst->wrr_cost[qindex + 1] = (uint8_t) wrr_cost[1];
592 dst->wrr_cost[qindex + 2] = (uint8_t) wrr_cost[2];
593 dst->wrr_cost[qindex + 3] = (uint8_t) wrr_cost[3];
596 rte_sched_port_log_pipe_profile(port, i);
599 port->pipe_tc3_rate_max = 0;
600 for (i = 0; i < port->n_pipe_profiles; i++) {
601 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
602 uint32_t pipe_tc3_rate = src->tc_rate[3];
604 if (port->pipe_tc3_rate_max < pipe_tc3_rate)
605 port->pipe_tc3_rate_max = pipe_tc3_rate;
609 struct rte_sched_port *
610 rte_sched_port_config(struct rte_sched_port_params *params)
612 struct rte_sched_port *port = NULL;
613 uint32_t mem_size, bmp_mem_size, n_queues_per_port, i;
615 /* Check user parameters. Determine the amount of memory to allocate */
616 mem_size = rte_sched_port_get_memory_footprint(params);
620 /* Allocate memory to store the data structures */
621 port = rte_zmalloc("qos_params", mem_size, RTE_CACHE_LINE_SIZE);
625 /* compile time checks */
626 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
627 RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS & (RTE_SCHED_PORT_N_GRINDERS - 1));
629 /* User parameters */
630 port->n_subports_per_port = params->n_subports_per_port;
631 port->n_pipes_per_subport = params->n_pipes_per_subport;
632 port->rate = params->rate;
633 port->mtu = params->mtu + params->frame_overhead;
634 port->frame_overhead = params->frame_overhead;
635 memcpy(port->qsize, params->qsize, sizeof(params->qsize));
636 port->n_pipe_profiles = params->n_pipe_profiles;
639 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
642 for (j = 0; j < e_RTE_METER_COLORS; j++) {
643 /* if min/max are both zero, then RED is disabled */
644 if ((params->red_params[i][j].min_th |
645 params->red_params[i][j].max_th) == 0) {
649 if (rte_red_config_init(&port->red_config[i][j],
650 params->red_params[i][j].wq_log2,
651 params->red_params[i][j].min_th,
652 params->red_params[i][j].max_th,
653 params->red_params[i][j].maxp_inv) != 0) {
661 port->time_cpu_cycles = rte_get_tsc_cycles();
662 port->time_cpu_bytes = 0;
664 port->cycles_per_byte = ((double) rte_get_tsc_hz()) / ((double) params->rate);
666 /* Scheduling loop detection */
667 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
668 port->pipe_exhaustion = 0;
671 port->busy_grinders = 0;
672 port->pkts_out = NULL;
673 port->n_pkts_out = 0;
675 /* Queue base calculation */
676 rte_sched_port_config_qsize(port);
678 /* Large data structures */
679 port->subport = (struct rte_sched_subport *)
680 (port->memory + rte_sched_port_get_array_base(params,
681 e_RTE_SCHED_PORT_ARRAY_SUBPORT));
682 port->pipe = (struct rte_sched_pipe *)
683 (port->memory + rte_sched_port_get_array_base(params,
684 e_RTE_SCHED_PORT_ARRAY_PIPE));
685 port->queue = (struct rte_sched_queue *)
686 (port->memory + rte_sched_port_get_array_base(params,
687 e_RTE_SCHED_PORT_ARRAY_QUEUE));
688 port->queue_extra = (struct rte_sched_queue_extra *)
689 (port->memory + rte_sched_port_get_array_base(params,
690 e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA));
691 port->pipe_profiles = (struct rte_sched_pipe_profile *)
692 (port->memory + rte_sched_port_get_array_base(params,
693 e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES));
694 port->bmp_array = port->memory
695 + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY);
696 port->queue_array = (struct rte_mbuf **)
697 (port->memory + rte_sched_port_get_array_base(params,
698 e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY));
700 /* Pipe profile table */
701 rte_sched_port_config_pipe_profile_table(port, params);
704 n_queues_per_port = rte_sched_port_queues_per_port(port);
705 bmp_mem_size = rte_bitmap_get_memory_footprint(n_queues_per_port);
706 port->bmp = rte_bitmap_init(n_queues_per_port, port->bmp_array,
708 if (port->bmp == NULL) {
709 RTE_LOG(ERR, SCHED, "Bitmap init error\n");
713 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
714 port->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
721 rte_sched_port_free(struct rte_sched_port *port)
725 /* Check user parameters */
729 /* Free enqueued mbufs */
730 for (queue = 0; queue < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; queue++) {
731 struct rte_mbuf **mbufs = rte_sched_port_qbase(port, queue);
734 for (i = 0; i < rte_sched_port_qsize(port, queue); i++)
735 rte_pktmbuf_free(mbufs[i]);
738 rte_bitmap_free(port->bmp);
743 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
745 struct rte_sched_subport *s = port->subport + i;
747 RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
748 " Token bucket: period = %u, credits per period = %u, size = %u\n"
749 " Traffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
750 " Traffic class 3 oversubscription: wm min = %u, wm max = %u\n",
755 s->tb_credits_per_period,
758 /* Traffic classes */
760 s->tc_credits_per_period[0],
761 s->tc_credits_per_period[1],
762 s->tc_credits_per_period[2],
763 s->tc_credits_per_period[3],
765 /* Traffic class 3 oversubscription */
771 rte_sched_subport_config(struct rte_sched_port *port,
773 struct rte_sched_subport_params *params)
775 struct rte_sched_subport *s;
778 /* Check user parameters */
780 subport_id >= port->n_subports_per_port ||
784 if (params->tb_rate == 0 || params->tb_rate > port->rate)
787 if (params->tb_size == 0)
790 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
791 if (params->tc_rate[i] == 0 ||
792 params->tc_rate[i] > params->tb_rate)
796 if (params->tc_period == 0)
799 s = port->subport + subport_id;
801 /* Token Bucket (TB) */
802 if (params->tb_rate == port->rate) {
803 s->tb_credits_per_period = 1;
806 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
807 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
809 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
812 s->tb_size = params->tb_size;
813 s->tb_time = port->time;
814 s->tb_credits = s->tb_size / 2;
816 /* Traffic Classes (TCs) */
817 s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
818 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
819 s->tc_credits_per_period[i]
820 = rte_sched_time_ms_to_bytes(params->tc_period,
823 s->tc_time = port->time + s->tc_period;
824 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
825 s->tc_credits[i] = s->tc_credits_per_period[i];
827 #ifdef RTE_SCHED_SUBPORT_TC_OV
828 /* TC oversubscription */
829 s->tc_ov_wm_min = port->mtu;
830 s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
831 port->pipe_tc3_rate_max);
832 s->tc_ov_wm = s->tc_ov_wm_max;
833 s->tc_ov_period_id = 0;
839 rte_sched_port_log_subport_config(port, subport_id);
845 rte_sched_pipe_config(struct rte_sched_port *port,
848 int32_t pipe_profile)
850 struct rte_sched_subport *s;
851 struct rte_sched_pipe *p;
852 struct rte_sched_pipe_profile *params;
853 uint32_t deactivate, profile, i;
855 /* Check user parameters */
856 profile = (uint32_t) pipe_profile;
857 deactivate = (pipe_profile < 0);
860 subport_id >= port->n_subports_per_port ||
861 pipe_id >= port->n_pipes_per_subport ||
862 (!deactivate && profile >= port->n_pipe_profiles))
866 /* Check that subport configuration is valid */
867 s = port->subport + subport_id;
868 if (s->tb_period == 0)
871 p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
873 /* Handle the case when pipe already has a valid configuration */
875 params = port->pipe_profiles + p->profile;
877 #ifdef RTE_SCHED_SUBPORT_TC_OV
878 double subport_tc3_rate = (double) s->tc_credits_per_period[3]
879 / (double) s->tc_period;
880 double pipe_tc3_rate = (double) params->tc_credits_per_period[3]
881 / (double) params->tc_period;
882 uint32_t tc3_ov = s->tc_ov;
884 /* Unplug pipe from its subport */
885 s->tc_ov_n -= params->tc_ov_weight;
886 s->tc_ov_rate -= pipe_tc3_rate;
887 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
889 if (s->tc_ov != tc3_ov) {
890 RTE_LOG(DEBUG, SCHED,
891 "Subport %u TC3 oversubscription is OFF (%.4lf >= %.4lf)\n",
892 subport_id, subport_tc3_rate, s->tc_ov_rate);
897 memset(p, 0, sizeof(struct rte_sched_pipe));
903 /* Apply the new pipe configuration */
904 p->profile = profile;
905 params = port->pipe_profiles + p->profile;
907 /* Token Bucket (TB) */
908 p->tb_time = port->time;
909 p->tb_credits = params->tb_size / 2;
911 /* Traffic Classes (TCs) */
912 p->tc_time = port->time + params->tc_period;
913 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
914 p->tc_credits[i] = params->tc_credits_per_period[i];
916 #ifdef RTE_SCHED_SUBPORT_TC_OV
918 /* Subport TC3 oversubscription */
919 double subport_tc3_rate = (double) s->tc_credits_per_period[3]
920 / (double) s->tc_period;
921 double pipe_tc3_rate = (double) params->tc_credits_per_period[3]
922 / (double) params->tc_period;
923 uint32_t tc3_ov = s->tc_ov;
925 s->tc_ov_n += params->tc_ov_weight;
926 s->tc_ov_rate += pipe_tc3_rate;
927 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
929 if (s->tc_ov != tc3_ov) {
930 RTE_LOG(DEBUG, SCHED,
931 "Subport %u TC3 oversubscription is ON (%.4lf < %.4lf)\n",
932 subport_id, subport_tc3_rate, s->tc_ov_rate);
934 p->tc_ov_period_id = s->tc_ov_period_id;
935 p->tc_ov_credits = s->tc_ov_wm;
943 rte_sched_port_pkt_write(struct rte_mbuf *pkt,
944 uint32_t subport, uint32_t pipe, uint32_t traffic_class,
945 uint32_t queue, enum rte_meter_color color)
947 struct rte_sched_port_hierarchy *sched
948 = (struct rte_sched_port_hierarchy *) &pkt->hash.sched;
950 RTE_BUILD_BUG_ON(sizeof(*sched) > sizeof(pkt->hash.sched));
952 sched->color = (uint32_t) color;
953 sched->subport = subport;
955 sched->traffic_class = traffic_class;
956 sched->queue = queue;
960 rte_sched_port_pkt_read_tree_path(const struct rte_mbuf *pkt,
961 uint32_t *subport, uint32_t *pipe,
962 uint32_t *traffic_class, uint32_t *queue)
964 const struct rte_sched_port_hierarchy *sched
965 = (const struct rte_sched_port_hierarchy *) &pkt->hash.sched;
967 *subport = sched->subport;
969 *traffic_class = sched->traffic_class;
970 *queue = sched->queue;
974 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
976 const struct rte_sched_port_hierarchy *sched
977 = (const struct rte_sched_port_hierarchy *) &pkt->hash.sched;
979 return (enum rte_meter_color) sched->color;
983 rte_sched_subport_read_stats(struct rte_sched_port *port,
985 struct rte_sched_subport_stats *stats,
988 struct rte_sched_subport *s;
990 /* Check user parameters */
991 if (port == NULL || subport_id >= port->n_subports_per_port ||
992 stats == NULL || tc_ov == NULL)
995 s = port->subport + subport_id;
997 /* Copy subport stats and clear */
998 memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
999 memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1001 /* Subport TC ovesubscription status */
1008 rte_sched_queue_read_stats(struct rte_sched_port *port,
1010 struct rte_sched_queue_stats *stats,
1013 struct rte_sched_queue *q;
1014 struct rte_sched_queue_extra *qe;
1016 /* Check user parameters */
1017 if ((port == NULL) ||
1018 (queue_id >= rte_sched_port_queues_per_port(port)) ||
1023 q = port->queue + queue_id;
1024 qe = port->queue_extra + queue_id;
1026 /* Copy queue stats and clear */
1027 memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1028 memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1031 *qlen = q->qw - q->qr;
1036 static inline uint32_t
1037 rte_sched_port_qindex(struct rte_sched_port *port, uint32_t subport, uint32_t pipe, uint32_t traffic_class, uint32_t queue)
1041 result = subport * port->n_pipes_per_subport + pipe;
1042 result = result * RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE + traffic_class;
1043 result = result * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
1048 #ifdef RTE_SCHED_DEBUG
1051 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1053 struct rte_sched_queue *queue = port->queue + qindex;
1055 return (queue->qr == queue->qw);
1058 #endif /* RTE_SCHED_DEBUG */
1060 #ifdef RTE_SCHED_COLLECT_STATS
1063 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1065 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1066 uint32_t tc_index = (qindex >> 2) & 0x3;
1067 uint32_t pkt_len = pkt->pkt_len;
1069 s->stats.n_pkts_tc[tc_index] += 1;
1070 s->stats.n_bytes_tc[tc_index] += pkt_len;
1074 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1076 struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1077 uint32_t tc_index = (qindex >> 2) & 0x3;
1078 uint32_t pkt_len = pkt->pkt_len;
1080 s->stats.n_pkts_tc_dropped[tc_index] += 1;
1081 s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1085 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1087 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1088 uint32_t pkt_len = pkt->pkt_len;
1090 qe->stats.n_pkts += 1;
1091 qe->stats.n_bytes += pkt_len;
1095 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1097 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1098 uint32_t pkt_len = pkt->pkt_len;
1100 qe->stats.n_pkts_dropped += 1;
1101 qe->stats.n_bytes_dropped += pkt_len;
1104 #endif /* RTE_SCHED_COLLECT_STATS */
1106 #ifdef RTE_SCHED_RED
1109 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1111 struct rte_sched_queue_extra *qe;
1112 struct rte_red_config *red_cfg;
1113 struct rte_red *red;
1115 enum rte_meter_color color;
1117 tc_index = (qindex >> 2) & 0x3;
1118 color = rte_sched_port_pkt_read_color(pkt);
1119 red_cfg = &port->red_config[tc_index][color];
1121 if ((red_cfg->min_th | red_cfg->max_th) == 0)
1124 qe = port->queue_extra + qindex;
1127 return rte_red_enqueue(red_cfg, red, qlen, port->time);
1131 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1133 struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1134 struct rte_red *red = &qe->red;
1136 rte_red_mark_queue_empty(red, port->time);
1141 #define rte_sched_port_red_drop(port, pkt, qindex, qlen) 0
1143 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1145 #endif /* RTE_SCHED_RED */
1147 #ifdef RTE_SCHED_DEBUG
1150 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1157 rte_panic("Empty slab at position %u\n", bmp_pos);
1160 for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1161 if (mask & bmp_slab) {
1162 if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1163 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1170 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1174 #endif /* RTE_SCHED_DEBUG */
1176 static inline uint32_t
1177 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port,
1178 struct rte_mbuf *pkt)
1180 struct rte_sched_queue *q;
1181 #ifdef RTE_SCHED_COLLECT_STATS
1182 struct rte_sched_queue_extra *qe;
1184 uint32_t subport, pipe, traffic_class, queue, qindex;
1186 rte_sched_port_pkt_read_tree_path(pkt, &subport, &pipe, &traffic_class, &queue);
1188 qindex = rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1189 q = port->queue + qindex;
1191 #ifdef RTE_SCHED_COLLECT_STATS
1192 qe = port->queue_extra + qindex;
1200 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1201 uint32_t qindex, struct rte_mbuf **qbase)
1203 struct rte_sched_queue *q;
1204 struct rte_mbuf **q_qw;
1207 q = port->queue + qindex;
1208 qsize = rte_sched_port_qsize(port, qindex);
1209 q_qw = qbase + (q->qw & (qsize - 1));
1211 rte_prefetch0(q_qw);
1212 rte_bitmap_prefetch0(port->bmp, qindex);
1216 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex,
1217 struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1219 struct rte_sched_queue *q;
1223 q = port->queue + qindex;
1224 qsize = rte_sched_port_qsize(port, qindex);
1225 qlen = q->qw - q->qr;
1227 /* Drop the packet (and update drop stats) when queue is full */
1228 if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) ||
1230 rte_pktmbuf_free(pkt);
1231 #ifdef RTE_SCHED_COLLECT_STATS
1232 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt);
1233 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt);
1238 /* Enqueue packet */
1239 qbase[q->qw & (qsize - 1)] = pkt;
1242 /* Activate queue in the port bitmap */
1243 rte_bitmap_set(port->bmp, qindex);
1246 #ifdef RTE_SCHED_COLLECT_STATS
1247 rte_sched_port_update_subport_stats(port, qindex, pkt);
1248 rte_sched_port_update_queue_stats(port, qindex, pkt);
1256 * The enqueue function implements a 4-level pipeline with each stage
1257 * processing two different packets. The purpose of using a pipeline
1258 * is to hide the latency of prefetching the data structures. The
1259 * naming convention is presented in the diagram below:
1261 * p00 _______ p10 _______ p20 _______ p30 _______
1262 * ----->| |----->| |----->| |----->| |----->
1263 * | 0 | | 1 | | 2 | | 3 |
1264 * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1269 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1272 struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1273 *pkt30, *pkt31, *pkt_last;
1274 struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1275 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1276 uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1277 uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1283 * Less then 6 input packets available, which is not enough to
1286 if (unlikely(n_pkts < 6)) {
1287 struct rte_mbuf **q_base[5];
1290 /* Prefetch the mbuf structure of each packet */
1291 for (i = 0; i < n_pkts; i++)
1292 rte_prefetch0(pkts[i]);
1294 /* Prefetch the queue structure for each queue */
1295 for (i = 0; i < n_pkts; i++)
1296 q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port,
1299 /* Prefetch the write pointer location of each queue */
1300 for (i = 0; i < n_pkts; i++) {
1301 q_base[i] = rte_sched_port_qbase(port, q[i]);
1302 rte_sched_port_enqueue_qwa_prefetch0(port, q[i],
1306 /* Write each packet to its queue */
1307 for (i = 0; i < n_pkts; i++)
1308 result += rte_sched_port_enqueue_qwa(port, q[i],
1309 q_base[i], pkts[i]);
1314 /* Feed the first 3 stages of the pipeline (6 packets needed) */
1317 rte_prefetch0(pkt20);
1318 rte_prefetch0(pkt21);
1322 rte_prefetch0(pkt10);
1323 rte_prefetch0(pkt11);
1325 q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1326 q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1330 rte_prefetch0(pkt00);
1331 rte_prefetch0(pkt01);
1333 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1334 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1336 q20_base = rte_sched_port_qbase(port, q20);
1337 q21_base = rte_sched_port_qbase(port, q21);
1338 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1339 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1341 /* Run the pipeline */
1342 for (i = 6; i < (n_pkts & (~1)); i += 2) {
1343 /* Propagate stage inputs */
1354 q30_base = q20_base;
1355 q31_base = q21_base;
1357 /* Stage 0: Get packets in */
1359 pkt01 = pkts[i + 1];
1360 rte_prefetch0(pkt00);
1361 rte_prefetch0(pkt01);
1363 /* Stage 1: Prefetch queue structure storing queue pointers */
1364 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1365 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1367 /* Stage 2: Prefetch queue write location */
1368 q20_base = rte_sched_port_qbase(port, q20);
1369 q21_base = rte_sched_port_qbase(port, q21);
1370 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1371 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1373 /* Stage 3: Write packet to queue and activate queue */
1374 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1375 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1376 result += r30 + r31;
1380 * Drain the pipeline (exactly 6 packets).
1381 * Handle the last packet in the case
1382 * of an odd number of input packets.
1384 pkt_last = pkts[n_pkts - 1];
1385 rte_prefetch0(pkt_last);
1387 q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1388 q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1390 q10_base = rte_sched_port_qbase(port, q10);
1391 q11_base = rte_sched_port_qbase(port, q11);
1392 rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1393 rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1395 r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1396 r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1397 result += r20 + r21;
1399 q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1401 q00_base = rte_sched_port_qbase(port, q00);
1402 q01_base = rte_sched_port_qbase(port, q01);
1403 rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1404 rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1406 r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1407 r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1408 result += r10 + r11;
1410 q_last_base = rte_sched_port_qbase(port, q_last);
1411 rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1413 r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1414 r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1415 result += r00 + r01;
1418 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1425 #ifndef RTE_SCHED_SUBPORT_TC_OV
1428 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1430 struct rte_sched_grinder *grinder = port->grinder + pos;
1431 struct rte_sched_subport *subport = grinder->subport;
1432 struct rte_sched_pipe *pipe = grinder->pipe;
1433 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1437 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1438 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1439 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1440 subport->tb_time += n_periods * subport->tb_period;
1443 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1444 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1445 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1446 pipe->tb_time += n_periods * params->tb_period;
1449 if (unlikely(port->time >= subport->tc_time)) {
1450 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1451 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1452 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1453 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1454 subport->tc_time = port->time + subport->tc_period;
1458 if (unlikely(port->time >= pipe->tc_time)) {
1459 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1460 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1461 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1462 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1463 pipe->tc_time = port->time + params->tc_period;
1469 static inline uint32_t
1470 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1472 struct rte_sched_grinder *grinder = port->grinder + pos;
1473 struct rte_sched_subport *subport = grinder->subport;
1474 uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1475 uint32_t tc_ov_consumption_max;
1476 uint32_t tc_ov_wm = subport->tc_ov_wm;
1478 if (subport->tc_ov == 0)
1479 return subport->tc_ov_wm_max;
1481 tc_ov_consumption[0] = subport->tc_credits_per_period[0] - subport->tc_credits[0];
1482 tc_ov_consumption[1] = subport->tc_credits_per_period[1] - subport->tc_credits[1];
1483 tc_ov_consumption[2] = subport->tc_credits_per_period[2] - subport->tc_credits[2];
1484 tc_ov_consumption[3] = subport->tc_credits_per_period[3] - subport->tc_credits[3];
1486 tc_ov_consumption_max = subport->tc_credits_per_period[3] -
1487 (tc_ov_consumption[0] + tc_ov_consumption[1] + tc_ov_consumption[2]);
1489 if (tc_ov_consumption[3] > (tc_ov_consumption_max - port->mtu)) {
1490 tc_ov_wm -= tc_ov_wm >> 7;
1491 if (tc_ov_wm < subport->tc_ov_wm_min)
1492 tc_ov_wm = subport->tc_ov_wm_min;
1497 tc_ov_wm += (tc_ov_wm >> 7) + 1;
1498 if (tc_ov_wm > subport->tc_ov_wm_max)
1499 tc_ov_wm = subport->tc_ov_wm_max;
1505 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1507 struct rte_sched_grinder *grinder = port->grinder + pos;
1508 struct rte_sched_subport *subport = grinder->subport;
1509 struct rte_sched_pipe *pipe = grinder->pipe;
1510 struct rte_sched_pipe_profile *params = grinder->pipe_params;
1514 n_periods = (port->time - subport->tb_time) / subport->tb_period;
1515 subport->tb_credits += n_periods * subport->tb_credits_per_period;
1516 subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1517 subport->tb_time += n_periods * subport->tb_period;
1520 n_periods = (port->time - pipe->tb_time) / params->tb_period;
1521 pipe->tb_credits += n_periods * params->tb_credits_per_period;
1522 pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1523 pipe->tb_time += n_periods * params->tb_period;
1526 if (unlikely(port->time >= subport->tc_time)) {
1527 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
1529 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1530 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1531 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1532 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1534 subport->tc_time = port->time + subport->tc_period;
1535 subport->tc_ov_period_id++;
1539 if (unlikely(port->time >= pipe->tc_time)) {
1540 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1541 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1542 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1543 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1544 pipe->tc_time = port->time + params->tc_period;
1547 /* Pipe TCs - Oversubscription */
1548 if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
1549 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
1551 pipe->tc_ov_period_id = subport->tc_ov_period_id;
1555 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
1558 #ifndef RTE_SCHED_SUBPORT_TC_OV
1561 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1563 struct rte_sched_grinder *grinder = port->grinder + pos;
1564 struct rte_sched_subport *subport = grinder->subport;
1565 struct rte_sched_pipe *pipe = grinder->pipe;
1566 struct rte_mbuf *pkt = grinder->pkt;
1567 uint32_t tc_index = grinder->tc_index;
1568 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1569 uint32_t subport_tb_credits = subport->tb_credits;
1570 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1571 uint32_t pipe_tb_credits = pipe->tb_credits;
1572 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1575 /* Check queue credits */
1576 enough_credits = (pkt_len <= subport_tb_credits) &&
1577 (pkt_len <= subport_tc_credits) &&
1578 (pkt_len <= pipe_tb_credits) &&
1579 (pkt_len <= pipe_tc_credits);
1581 if (!enough_credits)
1584 /* Update port credits */
1585 subport->tb_credits -= pkt_len;
1586 subport->tc_credits[tc_index] -= pkt_len;
1587 pipe->tb_credits -= pkt_len;
1588 pipe->tc_credits[tc_index] -= pkt_len;
1596 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1598 struct rte_sched_grinder *grinder = port->grinder + pos;
1599 struct rte_sched_subport *subport = grinder->subport;
1600 struct rte_sched_pipe *pipe = grinder->pipe;
1601 struct rte_mbuf *pkt = grinder->pkt;
1602 uint32_t tc_index = grinder->tc_index;
1603 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1604 uint32_t subport_tb_credits = subport->tb_credits;
1605 uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1606 uint32_t pipe_tb_credits = pipe->tb_credits;
1607 uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1608 uint32_t pipe_tc_ov_mask1[] = {UINT32_MAX, UINT32_MAX, UINT32_MAX, pipe->tc_ov_credits};
1609 uint32_t pipe_tc_ov_mask2[] = {0, 0, 0, UINT32_MAX};
1610 uint32_t pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
1613 /* Check pipe and subport credits */
1614 enough_credits = (pkt_len <= subport_tb_credits) &&
1615 (pkt_len <= subport_tc_credits) &&
1616 (pkt_len <= pipe_tb_credits) &&
1617 (pkt_len <= pipe_tc_credits) &&
1618 (pkt_len <= pipe_tc_ov_credits);
1620 if (!enough_credits)
1623 /* Update pipe and subport credits */
1624 subport->tb_credits -= pkt_len;
1625 subport->tc_credits[tc_index] -= pkt_len;
1626 pipe->tb_credits -= pkt_len;
1627 pipe->tc_credits[tc_index] -= pkt_len;
1628 pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
1633 #endif /* RTE_SCHED_SUBPORT_TC_OV */
1637 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
1639 struct rte_sched_grinder *grinder = port->grinder + pos;
1640 struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
1641 struct rte_mbuf *pkt = grinder->pkt;
1642 uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1644 if (!grinder_credits_check(port, pos))
1647 /* Advance port time */
1648 port->time += pkt_len;
1651 port->pkts_out[port->n_pkts_out++] = pkt;
1653 grinder->wrr_tokens[grinder->qpos] += pkt_len * grinder->wrr_cost[grinder->qpos];
1654 if (queue->qr == queue->qw) {
1655 uint32_t qindex = grinder->qindex[grinder->qpos];
1657 rte_bitmap_clear(port->bmp, qindex);
1658 grinder->qmask &= ~(1 << grinder->qpos);
1659 grinder->wrr_mask[grinder->qpos] = 0;
1660 rte_sched_port_set_queue_empty_timestamp(port, qindex);
1663 /* Reset pipe loop detection */
1664 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1665 grinder->productive = 1;
1670 #ifdef RTE_SCHED_VECTOR
1673 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1675 __m128i index = _mm_set1_epi32(base_pipe);
1676 __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
1677 __m128i res = _mm_cmpeq_epi32(pipes, index);
1679 pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
1680 pipes = _mm_cmpeq_epi32(pipes, index);
1681 res = _mm_or_si128(res, pipes);
1683 if (_mm_testz_si128(res, res))
1692 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1696 for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
1697 if (port->grinder_base_bmp_pos[i] == base_pipe)
1704 #endif /* RTE_SCHED_OPTIMIZATIONS */
1707 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
1709 struct rte_sched_grinder *grinder = port->grinder + pos;
1712 grinder->pcache_w = 0;
1713 grinder->pcache_r = 0;
1715 w[0] = (uint16_t) bmp_slab;
1716 w[1] = (uint16_t) (bmp_slab >> 16);
1717 w[2] = (uint16_t) (bmp_slab >> 32);
1718 w[3] = (uint16_t) (bmp_slab >> 48);
1720 grinder->pcache_qmask[grinder->pcache_w] = w[0];
1721 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
1722 grinder->pcache_w += (w[0] != 0);
1724 grinder->pcache_qmask[grinder->pcache_w] = w[1];
1725 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
1726 grinder->pcache_w += (w[1] != 0);
1728 grinder->pcache_qmask[grinder->pcache_w] = w[2];
1729 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
1730 grinder->pcache_w += (w[2] != 0);
1732 grinder->pcache_qmask[grinder->pcache_w] = w[3];
1733 grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
1734 grinder->pcache_w += (w[3] != 0);
1738 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
1740 struct rte_sched_grinder *grinder = port->grinder + pos;
1743 grinder->tccache_w = 0;
1744 grinder->tccache_r = 0;
1746 b[0] = (uint8_t) (qmask & 0xF);
1747 b[1] = (uint8_t) ((qmask >> 4) & 0xF);
1748 b[2] = (uint8_t) ((qmask >> 8) & 0xF);
1749 b[3] = (uint8_t) ((qmask >> 12) & 0xF);
1751 grinder->tccache_qmask[grinder->tccache_w] = b[0];
1752 grinder->tccache_qindex[grinder->tccache_w] = qindex;
1753 grinder->tccache_w += (b[0] != 0);
1755 grinder->tccache_qmask[grinder->tccache_w] = b[1];
1756 grinder->tccache_qindex[grinder->tccache_w] = qindex + 4;
1757 grinder->tccache_w += (b[1] != 0);
1759 grinder->tccache_qmask[grinder->tccache_w] = b[2];
1760 grinder->tccache_qindex[grinder->tccache_w] = qindex + 8;
1761 grinder->tccache_w += (b[2] != 0);
1763 grinder->tccache_qmask[grinder->tccache_w] = b[3];
1764 grinder->tccache_qindex[grinder->tccache_w] = qindex + 12;
1765 grinder->tccache_w += (b[3] != 0);
1769 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
1771 struct rte_sched_grinder *grinder = port->grinder + pos;
1772 struct rte_mbuf **qbase;
1776 if (grinder->tccache_r == grinder->tccache_w)
1779 qindex = grinder->tccache_qindex[grinder->tccache_r];
1780 qbase = rte_sched_port_qbase(port, qindex);
1781 qsize = rte_sched_port_qsize(port, qindex);
1783 grinder->tc_index = (qindex >> 2) & 0x3;
1784 grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
1785 grinder->qsize = qsize;
1787 grinder->qindex[0] = qindex;
1788 grinder->qindex[1] = qindex + 1;
1789 grinder->qindex[2] = qindex + 2;
1790 grinder->qindex[3] = qindex + 3;
1792 grinder->queue[0] = port->queue + qindex;
1793 grinder->queue[1] = port->queue + qindex + 1;
1794 grinder->queue[2] = port->queue + qindex + 2;
1795 grinder->queue[3] = port->queue + qindex + 3;
1797 grinder->qbase[0] = qbase;
1798 grinder->qbase[1] = qbase + qsize;
1799 grinder->qbase[2] = qbase + 2 * qsize;
1800 grinder->qbase[3] = qbase + 3 * qsize;
1802 grinder->tccache_r++;
1807 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
1809 struct rte_sched_grinder *grinder = port->grinder + pos;
1810 uint32_t pipe_qindex;
1811 uint16_t pipe_qmask;
1813 if (grinder->pcache_r < grinder->pcache_w) {
1814 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
1815 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
1816 grinder->pcache_r++;
1818 uint64_t bmp_slab = 0;
1819 uint32_t bmp_pos = 0;
1821 /* Get another non-empty pipe group */
1822 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
1825 #ifdef RTE_SCHED_DEBUG
1826 debug_check_queue_slab(port, bmp_pos, bmp_slab);
1829 /* Return if pipe group already in one of the other grinders */
1830 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
1831 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
1834 port->grinder_base_bmp_pos[pos] = bmp_pos;
1836 /* Install new pipe group into grinder's pipe cache */
1837 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
1839 pipe_qmask = grinder->pcache_qmask[0];
1840 pipe_qindex = grinder->pcache_qindex[0];
1841 grinder->pcache_r = 1;
1844 /* Install new pipe in the grinder */
1845 grinder->pindex = pipe_qindex >> 4;
1846 grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
1847 grinder->pipe = port->pipe + grinder->pindex;
1848 grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
1849 grinder->productive = 0;
1851 grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
1852 grinder_next_tc(port, pos);
1854 /* Check for pipe exhaustion */
1855 if (grinder->pindex == port->pipe_loop) {
1856 port->pipe_exhaustion = 1;
1857 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1865 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
1867 struct rte_sched_grinder *grinder = port->grinder + pos;
1868 struct rte_sched_pipe *pipe = grinder->pipe;
1869 struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
1870 uint32_t tc_index = grinder->tc_index;
1871 uint32_t qmask = grinder->qmask;
1874 qindex = tc_index * 4;
1876 grinder->wrr_tokens[0] = ((uint16_t) pipe->wrr_tokens[qindex]) << RTE_SCHED_WRR_SHIFT;
1877 grinder->wrr_tokens[1] = ((uint16_t) pipe->wrr_tokens[qindex + 1]) << RTE_SCHED_WRR_SHIFT;
1878 grinder->wrr_tokens[2] = ((uint16_t) pipe->wrr_tokens[qindex + 2]) << RTE_SCHED_WRR_SHIFT;
1879 grinder->wrr_tokens[3] = ((uint16_t) pipe->wrr_tokens[qindex + 3]) << RTE_SCHED_WRR_SHIFT;
1881 grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
1882 grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
1883 grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
1884 grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
1886 grinder->wrr_cost[0] = pipe_params->wrr_cost[qindex];
1887 grinder->wrr_cost[1] = pipe_params->wrr_cost[qindex + 1];
1888 grinder->wrr_cost[2] = pipe_params->wrr_cost[qindex + 2];
1889 grinder->wrr_cost[3] = pipe_params->wrr_cost[qindex + 3];
1893 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
1895 struct rte_sched_grinder *grinder = port->grinder + pos;
1896 struct rte_sched_pipe *pipe = grinder->pipe;
1897 uint32_t tc_index = grinder->tc_index;
1900 qindex = tc_index * 4;
1902 pipe->wrr_tokens[qindex] = (grinder->wrr_tokens[0] & grinder->wrr_mask[0])
1903 >> RTE_SCHED_WRR_SHIFT;
1904 pipe->wrr_tokens[qindex + 1] = (grinder->wrr_tokens[1] & grinder->wrr_mask[1])
1905 >> RTE_SCHED_WRR_SHIFT;
1906 pipe->wrr_tokens[qindex + 2] = (grinder->wrr_tokens[2] & grinder->wrr_mask[2])
1907 >> RTE_SCHED_WRR_SHIFT;
1908 pipe->wrr_tokens[qindex + 3] = (grinder->wrr_tokens[3] & grinder->wrr_mask[3])
1909 >> RTE_SCHED_WRR_SHIFT;
1913 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
1915 struct rte_sched_grinder *grinder = port->grinder + pos;
1916 uint16_t wrr_tokens_min;
1918 grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
1919 grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
1920 grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
1921 grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
1923 grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
1924 wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
1926 grinder->wrr_tokens[0] -= wrr_tokens_min;
1927 grinder->wrr_tokens[1] -= wrr_tokens_min;
1928 grinder->wrr_tokens[2] -= wrr_tokens_min;
1929 grinder->wrr_tokens[3] -= wrr_tokens_min;
1933 #define grinder_evict(port, pos)
1936 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
1938 struct rte_sched_grinder *grinder = port->grinder + pos;
1940 rte_prefetch0(grinder->pipe);
1941 rte_prefetch0(grinder->queue[0]);
1945 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
1947 struct rte_sched_grinder *grinder = port->grinder + pos;
1948 uint16_t qsize, qr[4];
1950 qsize = grinder->qsize;
1951 qr[0] = grinder->queue[0]->qr & (qsize - 1);
1952 qr[1] = grinder->queue[1]->qr & (qsize - 1);
1953 qr[2] = grinder->queue[2]->qr & (qsize - 1);
1954 qr[3] = grinder->queue[3]->qr & (qsize - 1);
1956 rte_prefetch0(grinder->qbase[0] + qr[0]);
1957 rte_prefetch0(grinder->qbase[1] + qr[1]);
1959 grinder_wrr_load(port, pos);
1960 grinder_wrr(port, pos);
1962 rte_prefetch0(grinder->qbase[2] + qr[2]);
1963 rte_prefetch0(grinder->qbase[3] + qr[3]);
1967 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
1969 struct rte_sched_grinder *grinder = port->grinder + pos;
1970 uint32_t qpos = grinder->qpos;
1971 struct rte_mbuf **qbase = grinder->qbase[qpos];
1972 uint16_t qsize = grinder->qsize;
1973 uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
1975 grinder->pkt = qbase[qr];
1976 rte_prefetch0(grinder->pkt);
1978 if (unlikely((qr & 0x7) == 7)) {
1979 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
1981 rte_prefetch0(qbase + qr_next);
1985 static inline uint32_t
1986 grinder_handle(struct rte_sched_port *port, uint32_t pos)
1988 struct rte_sched_grinder *grinder = port->grinder + pos;
1990 switch (grinder->state) {
1991 case e_GRINDER_PREFETCH_PIPE:
1993 if (grinder_next_pipe(port, pos)) {
1994 grinder_prefetch_pipe(port, pos);
1995 port->busy_grinders++;
1997 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2004 case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2006 struct rte_sched_pipe *pipe = grinder->pipe;
2008 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2009 grinder_prefetch_tc_queue_arrays(port, pos);
2010 grinder_credits_update(port, pos);
2012 grinder->state = e_GRINDER_PREFETCH_MBUF;
2016 case e_GRINDER_PREFETCH_MBUF:
2018 grinder_prefetch_mbuf(port, pos);
2020 grinder->state = e_GRINDER_READ_MBUF;
2024 case e_GRINDER_READ_MBUF:
2026 uint32_t result = 0;
2028 result = grinder_schedule(port, pos);
2030 /* Look for next packet within the same TC */
2031 if (result && grinder->qmask) {
2032 grinder_wrr(port, pos);
2033 grinder_prefetch_mbuf(port, pos);
2037 grinder_wrr_store(port, pos);
2039 /* Look for another active TC within same pipe */
2040 if (grinder_next_tc(port, pos)) {
2041 grinder_prefetch_tc_queue_arrays(port, pos);
2043 grinder->state = e_GRINDER_PREFETCH_MBUF;
2047 if (grinder->productive == 0 &&
2048 port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2049 port->pipe_loop = grinder->pindex;
2051 grinder_evict(port, pos);
2053 /* Look for another active pipe */
2054 if (grinder_next_pipe(port, pos)) {
2055 grinder_prefetch_pipe(port, pos);
2057 grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2061 /* No active pipe found */
2062 port->busy_grinders--;
2064 grinder->state = e_GRINDER_PREFETCH_PIPE;
2069 rte_panic("Algorithmic error (invalid state)\n");
2075 rte_sched_port_time_resync(struct rte_sched_port *port)
2077 uint64_t cycles = rte_get_tsc_cycles();
2078 uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2079 double bytes_diff = ((double) cycles_diff) / port->cycles_per_byte;
2081 /* Advance port time */
2082 port->time_cpu_cycles = cycles;
2083 port->time_cpu_bytes += (uint64_t) bytes_diff;
2084 if (port->time < port->time_cpu_bytes)
2085 port->time = port->time_cpu_bytes;
2087 /* Reset pipe loop detection */
2088 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2092 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2096 /* Check if any exception flag is set */
2097 exceptions = (second_pass && port->busy_grinders == 0) ||
2098 (port->pipe_exhaustion == 1);
2100 /* Clear exception flags */
2101 port->pipe_exhaustion = 0;
2107 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2111 port->pkts_out = pkts;
2112 port->n_pkts_out = 0;
2114 rte_sched_port_time_resync(port);
2116 /* Take each queue in the grinder one step further */
2117 for (i = 0, count = 0; ; i++) {
2118 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2119 if ((count == n_pkts) ||
2120 rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {