sched: remove pipe params config from port level
[dpdk.git] / lib / librte_sched / rte_sched.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #include <rte_common.h>
9 #include <rte_log.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>
15 #include <rte_mbuf.h>
16 #include <rte_bitmap.h>
17 #include <rte_reciprocal.h>
18
19 #include "rte_sched.h"
20 #include "rte_sched_common.h"
21 #include "rte_approx.h"
22
23 #ifdef __INTEL_COMPILER
24 #pragma warning(disable:2259) /* conversion may lose significant bits */
25 #endif
26
27 #ifdef RTE_SCHED_VECTOR
28 #include <rte_vect.h>
29
30 #ifdef RTE_ARCH_X86
31 #define SCHED_VECTOR_SSE4
32 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
33 #define SCHED_VECTOR_NEON
34 #endif
35
36 #endif
37
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
44
45 /* Scaling for cycles_per_byte calculation
46  * Chosen so that minimum rate is 480 bit/sec
47  */
48 #define RTE_SCHED_TIME_SHIFT                  8
49
50 struct rte_sched_pipe_profile {
51         /* Token bucket (TB) */
52         uint32_t tb_period;
53         uint32_t tb_credits_per_period;
54         uint32_t tb_size;
55
56         /* Pipe traffic classes */
57         uint32_t tc_period;
58         uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
59         uint8_t tc_ov_weight;
60
61         /* Pipe best-effort traffic class queues */
62         uint8_t  wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
63 };
64
65 struct rte_sched_pipe {
66         /* Token bucket (TB) */
67         uint64_t tb_time; /* time of last update */
68         uint32_t tb_credits;
69
70         /* Pipe profile and flags */
71         uint32_t profile;
72
73         /* Traffic classes (TCs) */
74         uint64_t tc_time; /* time of next update */
75         uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
76
77         /* Weighted Round Robin (WRR) */
78         uint8_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
79
80         /* TC oversubscription */
81         uint32_t tc_ov_credits;
82         uint8_t tc_ov_period_id;
83 } __rte_cache_aligned;
84
85 struct rte_sched_queue {
86         uint16_t qw;
87         uint16_t qr;
88 };
89
90 struct rte_sched_queue_extra {
91         struct rte_sched_queue_stats stats;
92 #ifdef RTE_SCHED_RED
93         struct rte_red red;
94 #endif
95 };
96
97 enum grinder_state {
98         e_GRINDER_PREFETCH_PIPE = 0,
99         e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
100         e_GRINDER_PREFETCH_MBUF,
101         e_GRINDER_READ_MBUF
102 };
103
104 struct rte_sched_grinder {
105         /* Pipe cache */
106         uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
107         uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
108         uint32_t pcache_w;
109         uint32_t pcache_r;
110
111         /* Current pipe */
112         enum grinder_state state;
113         uint32_t productive;
114         uint32_t pindex;
115         struct rte_sched_subport *subport;
116         struct rte_sched_pipe *pipe;
117         struct rte_sched_pipe_profile *pipe_params;
118
119         /* TC cache */
120         uint8_t tccache_qmask[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
121         uint32_t tccache_qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
122         uint32_t tccache_w;
123         uint32_t tccache_r;
124
125         /* Current TC */
126         uint32_t tc_index;
127         struct rte_sched_queue *queue[RTE_SCHED_MAX_QUEUES_PER_TC];
128         struct rte_mbuf **qbase[RTE_SCHED_MAX_QUEUES_PER_TC];
129         uint32_t qindex[RTE_SCHED_MAX_QUEUES_PER_TC];
130         uint16_t qsize;
131         uint32_t qmask;
132         uint32_t qpos;
133         struct rte_mbuf *pkt;
134
135         /* WRR */
136         uint16_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
137         uint16_t wrr_mask[RTE_SCHED_BE_QUEUES_PER_PIPE];
138         uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
139 };
140
141 struct rte_sched_subport {
142         /* Token bucket (TB) */
143         uint64_t tb_time; /* time of last update */
144         uint32_t tb_period;
145         uint32_t tb_credits_per_period;
146         uint32_t tb_size;
147         uint32_t tb_credits;
148
149         /* Traffic classes (TCs) */
150         uint64_t tc_time; /* time of next update */
151         uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
152         uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
153         uint32_t tc_period;
154
155         /* TC oversubscription */
156         uint32_t tc_ov_wm;
157         uint32_t tc_ov_wm_min;
158         uint32_t tc_ov_wm_max;
159         uint8_t tc_ov_period_id;
160         uint8_t tc_ov;
161         uint32_t tc_ov_n;
162         double tc_ov_rate;
163
164         /* Statistics */
165         struct rte_sched_subport_stats stats;
166
167         /* Subport pipes */
168         uint32_t n_pipes_per_subport_enabled;
169         uint32_t n_pipe_profiles;
170         uint32_t n_max_pipe_profiles;
171
172         /* Pipe best-effort TC rate */
173         uint32_t pipe_tc_be_rate_max;
174
175         /* Pipe queues size */
176         uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
177
178 #ifdef RTE_SCHED_RED
179         struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
180 #endif
181
182         /* Scheduling loop detection */
183         uint32_t pipe_loop;
184         uint32_t pipe_exhaustion;
185
186         /* Bitmap */
187         struct rte_bitmap *bmp;
188         uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
189
190         /* Grinders */
191         struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
192         uint32_t busy_grinders;
193
194         /* Queue base calculation */
195         uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
196         uint32_t qsize_sum;
197
198         struct rte_sched_pipe *pipe;
199         struct rte_sched_queue *queue;
200         struct rte_sched_queue_extra *queue_extra;
201         struct rte_sched_pipe_profile *pipe_profiles;
202         uint8_t *bmp_array;
203         struct rte_mbuf **queue_array;
204         uint8_t memory[0] __rte_cache_aligned;
205 } __rte_cache_aligned;
206
207 struct rte_sched_port {
208         /* User parameters */
209         uint32_t n_subports_per_port;
210         uint32_t n_pipes_per_subport;
211         uint32_t n_pipes_per_subport_log2;
212         uint16_t pipe_queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
213         uint8_t pipe_tc[RTE_SCHED_QUEUES_PER_PIPE];
214         uint8_t tc_queue[RTE_SCHED_QUEUES_PER_PIPE];
215         uint32_t rate;
216         uint32_t mtu;
217         uint32_t frame_overhead;
218         int socket;
219         uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
220         uint32_t n_pipe_profiles;
221         uint32_t n_max_pipe_profiles;
222         uint32_t pipe_tc_be_rate_max;
223 #ifdef RTE_SCHED_RED
224         struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
225 #endif
226
227         /* Timing */
228         uint64_t time_cpu_cycles;     /* Current CPU time measured in CPU cyles */
229         uint64_t time_cpu_bytes;      /* Current CPU time measured in bytes */
230         uint64_t time;                /* Current NIC TX time measured in bytes */
231         struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
232
233         /* Scheduling loop detection */
234         uint32_t pipe_loop;
235         uint32_t pipe_exhaustion;
236
237         /* Bitmap */
238         struct rte_bitmap *bmp;
239         uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
240
241         /* Grinders */
242         struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
243         uint32_t busy_grinders;
244         struct rte_mbuf **pkts_out;
245         uint32_t n_pkts_out;
246
247         /* Queue base calculation */
248         uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
249         uint32_t qsize_sum;
250
251         /* Large data structures */
252         struct rte_sched_subport *subports[0];
253         struct rte_sched_subport *subport;
254         struct rte_sched_pipe *pipe;
255         struct rte_sched_queue *queue;
256         struct rte_sched_queue_extra *queue_extra;
257         struct rte_sched_pipe_profile *pipe_profiles;
258         uint8_t *bmp_array;
259         struct rte_mbuf **queue_array;
260         uint8_t memory[0] __rte_cache_aligned;
261 } __rte_cache_aligned;
262
263 enum rte_sched_port_array {
264         e_RTE_SCHED_PORT_ARRAY_SUBPORT = 0,
265         e_RTE_SCHED_PORT_ARRAY_PIPE,
266         e_RTE_SCHED_PORT_ARRAY_QUEUE,
267         e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA,
268         e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES,
269         e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY,
270         e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY,
271         e_RTE_SCHED_PORT_ARRAY_TOTAL,
272 };
273
274 enum rte_sched_subport_array {
275         e_RTE_SCHED_SUBPORT_ARRAY_PIPE = 0,
276         e_RTE_SCHED_SUBPORT_ARRAY_QUEUE,
277         e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA,
278         e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES,
279         e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY,
280         e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY,
281         e_RTE_SCHED_SUBPORT_ARRAY_TOTAL,
282 };
283
284 #ifdef RTE_SCHED_COLLECT_STATS
285
286 static inline uint32_t
287 rte_sched_port_queues_per_subport(struct rte_sched_port *port)
288 {
289         return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport;
290 }
291
292 #endif
293
294 static inline uint32_t
295 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
296 {
297         return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
298 }
299
300 static inline struct rte_mbuf **
301 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
302 {
303         uint32_t pindex = qindex >> 4;
304         uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
305
306         return (subport->queue_array + pindex *
307                 subport->qsize_sum + subport->qsize_add[qpos]);
308 }
309
310 static inline uint16_t
311 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
312 struct rte_sched_subport *subport, uint32_t qindex)
313 {
314         uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
315
316         return subport->qsize[tc];
317 }
318
319 static inline uint32_t
320 rte_sched_port_queues_per_port(struct rte_sched_port *port)
321 {
322         return RTE_SCHED_QUEUES_PER_PIPE * port->n_pipes_per_subport * port->n_subports_per_port;
323 }
324
325 static inline struct rte_mbuf **
326 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
327 {
328         uint32_t pindex = qindex >> 4;
329         uint32_t qpos = qindex & 0xF;
330
331         return (port->queue_array + pindex *
332                 port->qsize_sum + port->qsize_add[qpos]);
333 }
334
335 static inline uint16_t
336 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
337 {
338         uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
339
340         return port->qsize[tc];
341 }
342
343 static inline uint16_t
344 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
345 {
346         uint16_t pipe_queue = port->pipe_queue[traffic_class];
347
348         return pipe_queue;
349 }
350
351 static inline uint8_t
352 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
353 {
354         uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
355
356         return pipe_tc;
357 }
358
359 static inline uint8_t
360 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
361 {
362         uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
363
364         return tc_queue;
365 }
366
367 static int
368 pipe_profile_check(struct rte_sched_pipe_params *params,
369         uint32_t rate, uint16_t *qsize)
370 {
371         uint32_t i;
372
373         /* Pipe parameters */
374         if (params == NULL) {
375                 RTE_LOG(ERR, SCHED,
376                         "%s: Incorrect value for parameter params\n", __func__);
377                 return -EINVAL;
378         }
379
380         /* TB rate: non-zero, not greater than port rate */
381         if (params->tb_rate == 0 ||
382                 params->tb_rate > rate) {
383                 RTE_LOG(ERR, SCHED,
384                         "%s: Incorrect value for tb rate\n", __func__);
385                 return -EINVAL;
386         }
387
388         /* TB size: non-zero */
389         if (params->tb_size == 0) {
390                 RTE_LOG(ERR, SCHED,
391                         "%s: Incorrect value for tb size\n", __func__);
392                 return -EINVAL;
393         }
394
395         /* TC rate: non-zero if qsize non-zero, less than pipe rate */
396         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
397                 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
398                         (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
399                         params->tc_rate[i] > params->tb_rate))) {
400                         RTE_LOG(ERR, SCHED,
401                                 "%s: Incorrect value for qsize or tc_rate\n", __func__);
402                         return -EINVAL;
403                 }
404         }
405
406         if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
407                 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
408                 RTE_LOG(ERR, SCHED,
409                         "%s: Incorrect value for be traffic class rate\n", __func__);
410                 return -EINVAL;
411         }
412
413         /* TC period: non-zero */
414         if (params->tc_period == 0) {
415                 RTE_LOG(ERR, SCHED,
416                         "%s: Incorrect value for tc period\n", __func__);
417                 return -EINVAL;
418         }
419
420         /*  Best effort tc oversubscription weight: non-zero */
421         if (params->tc_ov_weight == 0) {
422                 RTE_LOG(ERR, SCHED,
423                         "%s: Incorrect value for tc ov weight\n", __func__);
424                 return -EINVAL;
425         }
426
427         /* Queue WRR weights: non-zero */
428         for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
429                 if (params->wrr_weights[i] == 0) {
430                         RTE_LOG(ERR, SCHED,
431                                 "%s: Incorrect value for wrr weight\n", __func__);
432                         return -EINVAL;
433                 }
434         }
435
436         return 0;
437 }
438
439 static int
440 rte_sched_port_check_params(struct rte_sched_port_params *params)
441 {
442         if (params == NULL) {
443                 RTE_LOG(ERR, SCHED,
444                         "%s: Incorrect value for parameter params\n", __func__);
445                 return -EINVAL;
446         }
447
448         /* socket */
449         if (params->socket < 0) {
450                 RTE_LOG(ERR, SCHED,
451                         "%s: Incorrect value for socket id\n", __func__);
452                 return -EINVAL;
453         }
454
455         /* rate */
456         if (params->rate == 0) {
457                 RTE_LOG(ERR, SCHED,
458                         "%s: Incorrect value for rate\n", __func__);
459                 return -EINVAL;
460         }
461
462         /* mtu */
463         if (params->mtu == 0) {
464                 RTE_LOG(ERR, SCHED,
465                         "%s: Incorrect value for mtu\n", __func__);
466                 return -EINVAL;
467         }
468
469         /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
470         if (params->n_subports_per_port == 0 ||
471             params->n_subports_per_port > 1u << 16 ||
472             !rte_is_power_of_2(params->n_subports_per_port)) {
473                 RTE_LOG(ERR, SCHED,
474                         "%s: Incorrect value for number of subports\n", __func__);
475                 return -EINVAL;
476         }
477
478         /* n_pipes_per_subport: non-zero, power of 2 */
479         if (params->n_pipes_per_subport == 0 ||
480             !rte_is_power_of_2(params->n_pipes_per_subport)) {
481                 RTE_LOG(ERR, SCHED,
482                         "%s: Incorrect value for maximum pipes number\n", __func__);
483                 return -EINVAL;
484         }
485
486         return 0;
487 }
488
489 static uint32_t
490 rte_sched_port_get_array_base(struct rte_sched_port_params *params, enum rte_sched_port_array array)
491 {
492         uint32_t n_subports_per_port = params->n_subports_per_port;
493         uint32_t n_pipes_per_subport = params->n_pipes_per_subport;
494         uint32_t n_pipes_per_port = n_pipes_per_subport * n_subports_per_port;
495         uint32_t n_queues_per_port = RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport * n_subports_per_port;
496
497         uint32_t size_subport = n_subports_per_port * sizeof(struct rte_sched_subport);
498         uint32_t size_pipe = n_pipes_per_port * sizeof(struct rte_sched_pipe);
499         uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue);
500         uint32_t size_queue_extra
501                 = n_queues_per_port * sizeof(struct rte_sched_queue_extra);
502         uint32_t size_pipe_profiles
503                 = params->n_max_pipe_profiles * sizeof(struct rte_sched_pipe_profile);
504         uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port);
505         uint32_t size_per_pipe_queue_array, size_queue_array;
506
507         uint32_t base, i;
508
509         size_per_pipe_queue_array = 0;
510         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
511                 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
512                         size_per_pipe_queue_array +=
513                                 params->qsize[i] * sizeof(struct rte_mbuf *);
514                 else
515                         size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
516                                 params->qsize[i] * sizeof(struct rte_mbuf *);
517         }
518         size_queue_array = n_pipes_per_port * size_per_pipe_queue_array;
519
520         base = 0;
521
522         if (array == e_RTE_SCHED_PORT_ARRAY_SUBPORT)
523                 return base;
524         base += RTE_CACHE_LINE_ROUNDUP(size_subport);
525
526         if (array == e_RTE_SCHED_PORT_ARRAY_PIPE)
527                 return base;
528         base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
529
530         if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE)
531                 return base;
532         base += RTE_CACHE_LINE_ROUNDUP(size_queue);
533
534         if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA)
535                 return base;
536         base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
537
538         if (array == e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES)
539                 return base;
540         base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
541
542         if (array == e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY)
543                 return base;
544         base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
545
546         if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY)
547                 return base;
548         base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
549
550         return base;
551 }
552
553 uint32_t
554 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
555 {
556         uint32_t size0, size1;
557         int status;
558
559         status = rte_sched_port_check_params(params);
560         if (status != 0) {
561                 RTE_LOG(NOTICE, SCHED,
562                         "Port scheduler params check failed (%d)\n", status);
563
564                 return 0;
565         }
566
567         size0 = sizeof(struct rte_sched_port);
568         size1 = rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_TOTAL);
569
570         return size0 + size1;
571 }
572
573 static void
574 rte_sched_port_log_pipe_profile(struct rte_sched_port *port, uint32_t i)
575 {
576         struct rte_sched_pipe_profile *p = port->pipe_profiles + i;
577
578         RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
579                 "       Token bucket: period = %u, credits per period = %u, size = %u\n"
580                 "       Traffic classes: period = %u,\n"
581                 "       credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
582                 "       Best-effort traffic class oversubscription: weight = %hhu\n"
583                 "       WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
584                 i,
585
586                 /* Token bucket */
587                 p->tb_period,
588                 p->tb_credits_per_period,
589                 p->tb_size,
590
591                 /* Traffic classes */
592                 p->tc_period,
593                 p->tc_credits_per_period[0],
594                 p->tc_credits_per_period[1],
595                 p->tc_credits_per_period[2],
596                 p->tc_credits_per_period[3],
597                 p->tc_credits_per_period[4],
598                 p->tc_credits_per_period[5],
599                 p->tc_credits_per_period[6],
600                 p->tc_credits_per_period[7],
601                 p->tc_credits_per_period[8],
602                 p->tc_credits_per_period[9],
603                 p->tc_credits_per_period[10],
604                 p->tc_credits_per_period[11],
605                 p->tc_credits_per_period[12],
606
607                 /* Best-effort traffic class oversubscription */
608                 p->tc_ov_weight,
609
610                 /* WRR */
611                 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
612 }
613
614 static inline uint64_t
615 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
616 {
617         uint64_t time = time_ms;
618
619         time = (time * rate) / 1000;
620
621         return time;
622 }
623
624 static void
625 rte_sched_pipe_profile_convert(struct rte_sched_port *port,
626         struct rte_sched_pipe_params *src,
627         struct rte_sched_pipe_profile *dst,
628         uint32_t rate)
629 {
630         uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
631         uint32_t lcd1, lcd2, lcd;
632         uint32_t i;
633
634         /* Token Bucket */
635         if (src->tb_rate == rate) {
636                 dst->tb_credits_per_period = 1;
637                 dst->tb_period = 1;
638         } else {
639                 double tb_rate = (double) src->tb_rate
640                                 / (double) rate;
641                 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
642
643                 rte_approx(tb_rate, d,
644                         &dst->tb_credits_per_period, &dst->tb_period);
645         }
646
647         dst->tb_size = src->tb_size;
648
649         /* Traffic Classes */
650         dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
651                                                 rate);
652
653         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
654                 if (port->qsize[i])
655                         dst->tc_credits_per_period[i]
656                                 = rte_sched_time_ms_to_bytes(src->tc_period,
657                                         src->tc_rate[i]);
658
659         dst->tc_ov_weight = src->tc_ov_weight;
660
661         /* WRR queues */
662         wrr_cost[0] = src->wrr_weights[0];
663         wrr_cost[1] = src->wrr_weights[1];
664         wrr_cost[2] = src->wrr_weights[2];
665         wrr_cost[3] = src->wrr_weights[3];
666
667         lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
668         lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
669         lcd = rte_get_lcd(lcd1, lcd2);
670
671         wrr_cost[0] = lcd / wrr_cost[0];
672         wrr_cost[1] = lcd / wrr_cost[1];
673         wrr_cost[2] = lcd / wrr_cost[2];
674         wrr_cost[3] = lcd / wrr_cost[3];
675
676         dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
677         dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
678         dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
679         dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
680 }
681
682 struct rte_sched_port *
683 rte_sched_port_config(struct rte_sched_port_params *params)
684 {
685         struct rte_sched_port *port = NULL;
686         uint32_t size0, size1;
687         uint32_t cycles_per_byte;
688         uint32_t i, j;
689         int status;
690
691         status = rte_sched_port_check_params(params);
692         if (status != 0) {
693                 RTE_LOG(ERR, SCHED,
694                         "%s: Port scheduler params check failed (%d)\n",
695                         __func__, status);
696                 return NULL;
697         }
698
699         size0 = sizeof(struct rte_sched_port);
700         size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
701
702         /* Allocate memory to store the data structures */
703         port = rte_zmalloc_socket("qos_params", size0 + size1, RTE_CACHE_LINE_SIZE,
704                 params->socket);
705         if (port == NULL) {
706                 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
707
708                 return NULL;
709         }
710
711         /* User parameters */
712         port->n_subports_per_port = params->n_subports_per_port;
713         port->n_pipes_per_subport = params->n_pipes_per_subport;
714         port->n_pipes_per_subport_log2 =
715                         __builtin_ctz(params->n_pipes_per_subport);
716         port->socket = params->socket;
717
718         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
719                 port->pipe_queue[i] = i;
720
721         for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
722                 port->pipe_tc[i] = j;
723
724                 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
725                         j++;
726         }
727
728         for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
729                 port->tc_queue[i] = j;
730
731                 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
732                         j++;
733         }
734         port->rate = params->rate;
735         port->mtu = params->mtu + params->frame_overhead;
736         port->frame_overhead = params->frame_overhead;
737
738         /* Timing */
739         port->time_cpu_cycles = rte_get_tsc_cycles();
740         port->time_cpu_bytes = 0;
741         port->time = 0;
742
743         cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
744                 / params->rate;
745         port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
746
747         /* Grinders */
748         port->pkts_out = NULL;
749         port->n_pkts_out = 0;
750
751         return port;
752 }
753
754 static inline void
755 rte_sched_subport_free(struct rte_sched_port *port,
756         struct rte_sched_subport *subport)
757 {
758         uint32_t n_subport_pipe_queues;
759         uint32_t qindex;
760
761         if (subport == NULL)
762                 return;
763
764         n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
765
766         /* Free enqueued mbufs */
767         for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
768                 struct rte_mbuf **mbufs =
769                         rte_sched_subport_pipe_qbase(subport, qindex);
770                 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
771                 if (qsize != 0) {
772                         struct rte_sched_queue *queue = subport->queue + qindex;
773                         uint16_t qr = queue->qr & (qsize - 1);
774                         uint16_t qw = queue->qw & (qsize - 1);
775
776                         for (; qr != qw; qr = (qr + 1) & (qsize - 1))
777                                 rte_pktmbuf_free(mbufs[qr]);
778                 }
779         }
780
781         rte_bitmap_free(subport->bmp);
782 }
783
784 void
785 rte_sched_port_free(struct rte_sched_port *port)
786 {
787         uint32_t i;
788
789         /* Check user parameters */
790         if (port == NULL)
791                 return;
792
793         for (i = 0; i < port->n_subports_per_port; i++)
794                 rte_sched_subport_free(port, port->subports[i]);
795
796         rte_free(port);
797 }
798
799 static void
800 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
801 {
802         struct rte_sched_subport *s = port->subport + i;
803
804         RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
805                 "       Token bucket: period = %u, credits per period = %u, size = %u\n"
806                 "       Traffic classes: period = %u\n"
807                 "       credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
808                 "       Best effort traffic class oversubscription: wm min = %u, wm max = %u\n",
809                 i,
810
811                 /* Token bucket */
812                 s->tb_period,
813                 s->tb_credits_per_period,
814                 s->tb_size,
815
816                 /* Traffic classes */
817                 s->tc_period,
818                 s->tc_credits_per_period[0],
819                 s->tc_credits_per_period[1],
820                 s->tc_credits_per_period[2],
821                 s->tc_credits_per_period[3],
822                 s->tc_credits_per_period[4],
823                 s->tc_credits_per_period[5],
824                 s->tc_credits_per_period[6],
825                 s->tc_credits_per_period[7],
826                 s->tc_credits_per_period[8],
827                 s->tc_credits_per_period[9],
828                 s->tc_credits_per_period[10],
829                 s->tc_credits_per_period[11],
830                 s->tc_credits_per_period[12],
831
832                 /* Best effort traffic class oversubscription */
833                 s->tc_ov_wm_min,
834                 s->tc_ov_wm_max);
835 }
836
837 int
838 rte_sched_subport_config(struct rte_sched_port *port,
839         uint32_t subport_id,
840         struct rte_sched_subport_params *params)
841 {
842         struct rte_sched_subport *s;
843         uint32_t i;
844
845         /* Check user parameters */
846         if (port == NULL) {
847                 RTE_LOG(ERR, SCHED,
848                         "%s: Incorrect value for parameter port\n", __func__);
849                 return -EINVAL;
850         }
851
852         if (subport_id >= port->n_subports_per_port) {
853                 RTE_LOG(ERR, SCHED,
854                         "%s: Incorrect value for subport id\n", __func__);
855                 return -EINVAL;
856         }
857
858         if (params == NULL) {
859                 RTE_LOG(ERR, SCHED,
860                         "%s: Incorrect value for parameter params\n", __func__);
861                 return -EINVAL;
862         }
863
864         if (params->tb_rate == 0 || params->tb_rate > port->rate) {
865                 RTE_LOG(ERR, SCHED,
866                         "%s: Incorrect value for tb rate\n", __func__);
867                 return -EINVAL;
868         }
869
870         if (params->tb_size == 0) {
871                 RTE_LOG(ERR, SCHED,
872                         "%s: Incorrect value for tb size\n", __func__);
873                 return -EINVAL;
874         }
875
876         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
877                 uint32_t tc_rate = params->tc_rate[i];
878                 uint16_t qsize = port->qsize[i];
879
880                 if ((qsize == 0 && tc_rate != 0) ||
881                         (qsize != 0 && tc_rate == 0) ||
882                         (tc_rate > params->tb_rate)) {
883                         RTE_LOG(ERR, SCHED,
884                                 "%s: Incorrect value for tc rate\n", __func__);
885                         return -EINVAL;
886                 }
887         }
888
889         if (port->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
890                 params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
891                 RTE_LOG(ERR, SCHED,
892                         "%s: Incorrect value for tc rate(best effort)\n", __func__);
893                 return -EINVAL;
894         }
895
896         if (params->tc_period == 0) {
897                 RTE_LOG(ERR, SCHED,
898                         "%s: Incorrect value for tc period\n", __func__);
899                 return -EINVAL;
900         }
901
902         s = port->subport + subport_id;
903
904         /* Token Bucket (TB) */
905         if (params->tb_rate == port->rate) {
906                 s->tb_credits_per_period = 1;
907                 s->tb_period = 1;
908         } else {
909                 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
910                 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
911
912                 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
913         }
914
915         s->tb_size = params->tb_size;
916         s->tb_time = port->time;
917         s->tb_credits = s->tb_size / 2;
918
919         /* Traffic Classes (TCs) */
920         s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
921         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
922                 if (port->qsize[i])
923                         s->tc_credits_per_period[i]
924                                 = rte_sched_time_ms_to_bytes(params->tc_period,
925                                                                  params->tc_rate[i]);
926
927         }
928         s->tc_time = port->time + s->tc_period;
929         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
930                 if (port->qsize[i])
931                         s->tc_credits[i] = s->tc_credits_per_period[i];
932
933         /* TC oversubscription */
934         s->tc_ov_wm_min = port->mtu;
935         s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
936                                                      port->pipe_tc_be_rate_max);
937         s->tc_ov_wm = s->tc_ov_wm_max;
938         s->tc_ov_period_id = 0;
939         s->tc_ov = 0;
940         s->tc_ov_n = 0;
941         s->tc_ov_rate = 0;
942
943         rte_sched_port_log_subport_config(port, subport_id);
944
945         return 0;
946 }
947
948 int
949 rte_sched_pipe_config(struct rte_sched_port *port,
950         uint32_t subport_id,
951         uint32_t pipe_id,
952         int32_t pipe_profile)
953 {
954         struct rte_sched_subport *s;
955         struct rte_sched_pipe *p;
956         struct rte_sched_pipe_profile *params;
957         uint32_t deactivate, profile, i;
958
959         /* Check user parameters */
960         profile = (uint32_t) pipe_profile;
961         deactivate = (pipe_profile < 0);
962
963         if (port == NULL) {
964                 RTE_LOG(ERR, SCHED,
965                         "%s: Incorrect value for parameter port\n", __func__);
966                 return -EINVAL;
967         }
968
969         if (subport_id >= port->n_subports_per_port) {
970                 RTE_LOG(ERR, SCHED,
971                         "%s: Incorrect value for parameter subport id\n", __func__);
972                 return -EINVAL;
973         }
974
975         if (pipe_id >= port->n_pipes_per_subport) {
976                 RTE_LOG(ERR, SCHED,
977                         "%s: Incorrect value for parameter pipe id\n", __func__);
978                 return -EINVAL;
979         }
980
981         if (!deactivate && profile >= port->n_pipe_profiles) {
982                 RTE_LOG(ERR, SCHED,
983                         "%s: Incorrect value for parameter pipe profile\n", __func__);
984                 return -EINVAL;
985         }
986
987         /* Check that subport configuration is valid */
988         s = port->subport + subport_id;
989         if (s->tb_period == 0) {
990                 RTE_LOG(ERR, SCHED,
991                         "%s: Subport configuration invalid\n", __func__);
992                 return -EINVAL;
993         }
994
995         p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
996
997         /* Handle the case when pipe already has a valid configuration */
998         if (p->tb_time) {
999                 params = port->pipe_profiles + p->profile;
1000
1001                 double subport_tc_be_rate =
1002                         (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1003                         / (double) s->tc_period;
1004                 double pipe_tc_be_rate =
1005                         (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1006                         / (double) params->tc_period;
1007                 uint32_t tc_be_ov = s->tc_ov;
1008
1009                 /* Unplug pipe from its subport */
1010                 s->tc_ov_n -= params->tc_ov_weight;
1011                 s->tc_ov_rate -= pipe_tc_be_rate;
1012                 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1013
1014                 if (s->tc_ov != tc_be_ov) {
1015                         RTE_LOG(DEBUG, SCHED,
1016                                 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1017                                 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1018                 }
1019
1020                 /* Reset the pipe */
1021                 memset(p, 0, sizeof(struct rte_sched_pipe));
1022         }
1023
1024         if (deactivate)
1025                 return 0;
1026
1027         /* Apply the new pipe configuration */
1028         p->profile = profile;
1029         params = port->pipe_profiles + p->profile;
1030
1031         /* Token Bucket (TB) */
1032         p->tb_time = port->time;
1033         p->tb_credits = params->tb_size / 2;
1034
1035         /* Traffic Classes (TCs) */
1036         p->tc_time = port->time + params->tc_period;
1037
1038         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1039                 if (port->qsize[i])
1040                         p->tc_credits[i] = params->tc_credits_per_period[i];
1041
1042         {
1043                 /* Subport best effort tc oversubscription */
1044                 double subport_tc_be_rate =
1045                         (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1046                         / (double) s->tc_period;
1047                 double pipe_tc_be_rate =
1048                         (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1049                         / (double) params->tc_period;
1050                 uint32_t tc_be_ov = s->tc_ov;
1051
1052                 s->tc_ov_n += params->tc_ov_weight;
1053                 s->tc_ov_rate += pipe_tc_be_rate;
1054                 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1055
1056                 if (s->tc_ov != tc_be_ov) {
1057                         RTE_LOG(DEBUG, SCHED,
1058                                 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1059                                 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1060                 }
1061                 p->tc_ov_period_id = s->tc_ov_period_id;
1062                 p->tc_ov_credits = s->tc_ov_wm;
1063         }
1064
1065         return 0;
1066 }
1067
1068 int
1069 rte_sched_port_pipe_profile_add(struct rte_sched_port *port,
1070         struct rte_sched_pipe_params *params,
1071         uint32_t *pipe_profile_id)
1072 {
1073         struct rte_sched_pipe_profile *pp;
1074         uint32_t i;
1075         int status;
1076
1077         /* Port */
1078         if (port == NULL) {
1079                 RTE_LOG(ERR, SCHED,
1080                         "%s: Incorrect value for parameter port\n", __func__);
1081                 return -EINVAL;
1082         }
1083
1084         /* Pipe profiles not exceeds the max limit */
1085         if (port->n_pipe_profiles >= port->n_max_pipe_profiles) {
1086                 RTE_LOG(ERR, SCHED,
1087                         "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1088                 return -EINVAL;
1089         }
1090
1091         /* Pipe params */
1092         status = pipe_profile_check(params, port->rate, &port->qsize[0]);
1093         if (status != 0) {
1094                 RTE_LOG(ERR, SCHED,
1095                         "%s: Pipe profile check failed(%d)\n", __func__, status);
1096                 return -EINVAL;
1097         }
1098
1099         pp = &port->pipe_profiles[port->n_pipe_profiles];
1100         rte_sched_pipe_profile_convert(port, params, pp, port->rate);
1101
1102         /* Pipe profile not exists */
1103         for (i = 0; i < port->n_pipe_profiles; i++)
1104                 if (memcmp(port->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1105                         RTE_LOG(ERR, SCHED,
1106                                 "%s: Pipe profile doesn't exist\n", __func__);
1107                         return -EINVAL;
1108                 }
1109
1110         /* Pipe profile commit */
1111         *pipe_profile_id = port->n_pipe_profiles;
1112         port->n_pipe_profiles++;
1113
1114         if (port->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1115                 port->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1116
1117         rte_sched_port_log_pipe_profile(port, *pipe_profile_id);
1118
1119         return 0;
1120 }
1121
1122 static inline uint32_t
1123 rte_sched_port_qindex(struct rte_sched_port *port,
1124         uint32_t subport,
1125         uint32_t pipe,
1126         uint32_t traffic_class,
1127         uint32_t queue)
1128 {
1129         return ((subport & (port->n_subports_per_port - 1)) <<
1130                         (port->n_pipes_per_subport_log2 + 4)) |
1131                         ((pipe & (port->n_pipes_per_subport - 1)) << 4) |
1132                         ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1133                         (RTE_SCHED_QUEUES_PER_PIPE - 1));
1134 }
1135
1136 void
1137 rte_sched_port_pkt_write(struct rte_sched_port *port,
1138                          struct rte_mbuf *pkt,
1139                          uint32_t subport, uint32_t pipe,
1140                          uint32_t traffic_class,
1141                          uint32_t queue, enum rte_color color)
1142 {
1143         uint32_t queue_id =
1144                 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1145
1146         rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1147 }
1148
1149 void
1150 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1151                                   const struct rte_mbuf *pkt,
1152                                   uint32_t *subport, uint32_t *pipe,
1153                                   uint32_t *traffic_class, uint32_t *queue)
1154 {
1155         uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1156
1157         *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1158         *pipe = (queue_id >> 4) & (port->n_pipes_per_subport - 1);
1159         *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1160         *queue = rte_sched_port_tc_queue(port, queue_id);
1161 }
1162
1163 enum rte_color
1164 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1165 {
1166         return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1167 }
1168
1169 int
1170 rte_sched_subport_read_stats(struct rte_sched_port *port,
1171                              uint32_t subport_id,
1172                              struct rte_sched_subport_stats *stats,
1173                              uint32_t *tc_ov)
1174 {
1175         struct rte_sched_subport *s;
1176
1177         /* Check user parameters */
1178         if (port == NULL) {
1179                 RTE_LOG(ERR, SCHED,
1180                         "%s: Incorrect value for parameter port\n", __func__);
1181                 return -EINVAL;
1182         }
1183
1184         if (subport_id >= port->n_subports_per_port) {
1185                 RTE_LOG(ERR, SCHED,
1186                         "%s: Incorrect value for subport id\n", __func__);
1187                 return -EINVAL;
1188         }
1189
1190         if (stats == NULL) {
1191                 RTE_LOG(ERR, SCHED,
1192                         "%s: Incorrect value for parameter stats\n", __func__);
1193                 return -EINVAL;
1194         }
1195
1196         if (tc_ov == NULL) {
1197                 RTE_LOG(ERR, SCHED,
1198                         "%s: Incorrect value for tc_ov\n", __func__);
1199                 return -EINVAL;
1200         }
1201
1202         s = port->subport + subport_id;
1203
1204         /* Copy subport stats and clear */
1205         memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1206         memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1207
1208         /* Subport TC oversubscription status */
1209         *tc_ov = s->tc_ov;
1210
1211         return 0;
1212 }
1213
1214 int
1215 rte_sched_queue_read_stats(struct rte_sched_port *port,
1216         uint32_t queue_id,
1217         struct rte_sched_queue_stats *stats,
1218         uint16_t *qlen)
1219 {
1220         struct rte_sched_queue *q;
1221         struct rte_sched_queue_extra *qe;
1222
1223         /* Check user parameters */
1224         if (port == NULL) {
1225                 RTE_LOG(ERR, SCHED,
1226                         "%s: Incorrect value for parameter port\n", __func__);
1227                 return -EINVAL;
1228         }
1229
1230         if (queue_id >= rte_sched_port_queues_per_port(port)) {
1231                 RTE_LOG(ERR, SCHED,
1232                         "%s: Incorrect value for queue id\n", __func__);
1233                 return -EINVAL;
1234         }
1235
1236         if (stats == NULL) {
1237                 RTE_LOG(ERR, SCHED,
1238                         "%s: Incorrect value for parameter stats\n", __func__);
1239                 return -EINVAL;
1240         }
1241
1242         if (qlen == NULL) {
1243                 RTE_LOG(ERR, SCHED,
1244                         "%s: Incorrect value for parameter qlen\n", __func__);
1245                 return -EINVAL;
1246         }
1247         q = port->queue + queue_id;
1248         qe = port->queue_extra + queue_id;
1249
1250         /* Copy queue stats and clear */
1251         memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1252         memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1253
1254         /* Queue length */
1255         *qlen = q->qw - q->qr;
1256
1257         return 0;
1258 }
1259
1260 #ifdef RTE_SCHED_DEBUG
1261
1262 static inline int
1263 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1264 {
1265         struct rte_sched_queue *queue = port->queue + qindex;
1266
1267         return queue->qr == queue->qw;
1268 }
1269
1270 #endif /* RTE_SCHED_DEBUG */
1271
1272 #ifdef RTE_SCHED_COLLECT_STATS
1273
1274 static inline void
1275 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1276 {
1277         struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1278         uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1279         uint32_t pkt_len = pkt->pkt_len;
1280
1281         s->stats.n_pkts_tc[tc_index] += 1;
1282         s->stats.n_bytes_tc[tc_index] += pkt_len;
1283 }
1284
1285 #ifdef RTE_SCHED_RED
1286 static inline void
1287 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1288                                                 uint32_t qindex,
1289                                                 struct rte_mbuf *pkt, uint32_t red)
1290 #else
1291 static inline void
1292 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1293                                                 uint32_t qindex,
1294                                                 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1295 #endif
1296 {
1297         struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1298         uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1299         uint32_t pkt_len = pkt->pkt_len;
1300
1301         s->stats.n_pkts_tc_dropped[tc_index] += 1;
1302         s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1303 #ifdef RTE_SCHED_RED
1304         s->stats.n_pkts_red_dropped[tc_index] += red;
1305 #endif
1306 }
1307
1308 static inline void
1309 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1310 {
1311         struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1312         uint32_t pkt_len = pkt->pkt_len;
1313
1314         qe->stats.n_pkts += 1;
1315         qe->stats.n_bytes += pkt_len;
1316 }
1317
1318 #ifdef RTE_SCHED_RED
1319 static inline void
1320 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1321                                                 uint32_t qindex,
1322                                                 struct rte_mbuf *pkt, uint32_t red)
1323 #else
1324 static inline void
1325 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1326                                                 uint32_t qindex,
1327                                                 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1328 #endif
1329 {
1330         struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1331         uint32_t pkt_len = pkt->pkt_len;
1332
1333         qe->stats.n_pkts_dropped += 1;
1334         qe->stats.n_bytes_dropped += pkt_len;
1335 #ifdef RTE_SCHED_RED
1336         qe->stats.n_pkts_red_dropped += red;
1337 #endif
1338 }
1339
1340 #endif /* RTE_SCHED_COLLECT_STATS */
1341
1342 #ifdef RTE_SCHED_RED
1343
1344 static inline int
1345 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1346 {
1347         struct rte_sched_queue_extra *qe;
1348         struct rte_red_config *red_cfg;
1349         struct rte_red *red;
1350         uint32_t tc_index;
1351         enum rte_color color;
1352
1353         tc_index = rte_sched_port_pipe_tc(port, qindex);
1354         color = rte_sched_port_pkt_read_color(pkt);
1355         red_cfg = &port->red_config[tc_index][color];
1356
1357         if ((red_cfg->min_th | red_cfg->max_th) == 0)
1358                 return 0;
1359
1360         qe = port->queue_extra + qindex;
1361         red = &qe->red;
1362
1363         return rte_red_enqueue(red_cfg, red, qlen, port->time);
1364 }
1365
1366 static inline void
1367 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1368 {
1369         struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1370         struct rte_red *red = &qe->red;
1371
1372         rte_red_mark_queue_empty(red, port->time);
1373 }
1374
1375 #else
1376
1377 #define rte_sched_port_red_drop(port, pkt, qindex, qlen)             0
1378
1379 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1380
1381 #endif /* RTE_SCHED_RED */
1382
1383 #ifdef RTE_SCHED_DEBUG
1384
1385 static inline void
1386 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1387                        uint64_t bmp_slab)
1388 {
1389         uint64_t mask;
1390         uint32_t i, panic;
1391
1392         if (bmp_slab == 0)
1393                 rte_panic("Empty slab at position %u\n", bmp_pos);
1394
1395         panic = 0;
1396         for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1397                 if (mask & bmp_slab) {
1398                         if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1399                                 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1400                                 panic = 1;
1401                         }
1402                 }
1403         }
1404
1405         if (panic)
1406                 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1407                         bmp_slab, bmp_pos);
1408 }
1409
1410 #endif /* RTE_SCHED_DEBUG */
1411
1412 static inline uint32_t
1413 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port,
1414                                        struct rte_mbuf *pkt)
1415 {
1416         struct rte_sched_queue *q;
1417 #ifdef RTE_SCHED_COLLECT_STATS
1418         struct rte_sched_queue_extra *qe;
1419 #endif
1420         uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1421
1422         q = port->queue + qindex;
1423         rte_prefetch0(q);
1424 #ifdef RTE_SCHED_COLLECT_STATS
1425         qe = port->queue_extra + qindex;
1426         rte_prefetch0(qe);
1427 #endif
1428
1429         return qindex;
1430 }
1431
1432 static inline void
1433 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1434                                      uint32_t qindex, struct rte_mbuf **qbase)
1435 {
1436         struct rte_sched_queue *q;
1437         struct rte_mbuf **q_qw;
1438         uint16_t qsize;
1439
1440         q = port->queue + qindex;
1441         qsize = rte_sched_port_qsize(port, qindex);
1442         q_qw = qbase + (q->qw & (qsize - 1));
1443
1444         rte_prefetch0(q_qw);
1445         rte_bitmap_prefetch0(port->bmp, qindex);
1446 }
1447
1448 static inline int
1449 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex,
1450                            struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1451 {
1452         struct rte_sched_queue *q;
1453         uint16_t qsize;
1454         uint16_t qlen;
1455
1456         q = port->queue + qindex;
1457         qsize = rte_sched_port_qsize(port, qindex);
1458         qlen = q->qw - q->qr;
1459
1460         /* Drop the packet (and update drop stats) when queue is full */
1461         if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) ||
1462                      (qlen >= qsize))) {
1463                 rte_pktmbuf_free(pkt);
1464 #ifdef RTE_SCHED_COLLECT_STATS
1465                 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt,
1466                                                             qlen < qsize);
1467                 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt,
1468                                                           qlen < qsize);
1469 #endif
1470                 return 0;
1471         }
1472
1473         /* Enqueue packet */
1474         qbase[q->qw & (qsize - 1)] = pkt;
1475         q->qw++;
1476
1477         /* Activate queue in the port bitmap */
1478         rte_bitmap_set(port->bmp, qindex);
1479
1480         /* Statistics */
1481 #ifdef RTE_SCHED_COLLECT_STATS
1482         rte_sched_port_update_subport_stats(port, qindex, pkt);
1483         rte_sched_port_update_queue_stats(port, qindex, pkt);
1484 #endif
1485
1486         return 1;
1487 }
1488
1489
1490 /*
1491  * The enqueue function implements a 4-level pipeline with each stage
1492  * processing two different packets. The purpose of using a pipeline
1493  * is to hide the latency of prefetching the data structures. The
1494  * naming convention is presented in the diagram below:
1495  *
1496  *   p00  _______   p10  _______   p20  _______   p30  _______
1497  * ----->|       |----->|       |----->|       |----->|       |----->
1498  *       |   0   |      |   1   |      |   2   |      |   3   |
1499  * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1500  *   p01            p11            p21            p31
1501  *
1502  */
1503 int
1504 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1505                        uint32_t n_pkts)
1506 {
1507         struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1508                 *pkt30, *pkt31, *pkt_last;
1509         struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1510                 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1511         uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1512         uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1513         uint32_t result, i;
1514
1515         result = 0;
1516
1517         /*
1518          * Less then 6 input packets available, which is not enough to
1519          * feed the pipeline
1520          */
1521         if (unlikely(n_pkts < 6)) {
1522                 struct rte_mbuf **q_base[5];
1523                 uint32_t q[5];
1524
1525                 /* Prefetch the mbuf structure of each packet */
1526                 for (i = 0; i < n_pkts; i++)
1527                         rte_prefetch0(pkts[i]);
1528
1529                 /* Prefetch the queue structure for each queue */
1530                 for (i = 0; i < n_pkts; i++)
1531                         q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port,
1532                                                                       pkts[i]);
1533
1534                 /* Prefetch the write pointer location of each queue */
1535                 for (i = 0; i < n_pkts; i++) {
1536                         q_base[i] = rte_sched_port_qbase(port, q[i]);
1537                         rte_sched_port_enqueue_qwa_prefetch0(port, q[i],
1538                                                              q_base[i]);
1539                 }
1540
1541                 /* Write each packet to its queue */
1542                 for (i = 0; i < n_pkts; i++)
1543                         result += rte_sched_port_enqueue_qwa(port, q[i],
1544                                                              q_base[i], pkts[i]);
1545
1546                 return result;
1547         }
1548
1549         /* Feed the first 3 stages of the pipeline (6 packets needed) */
1550         pkt20 = pkts[0];
1551         pkt21 = pkts[1];
1552         rte_prefetch0(pkt20);
1553         rte_prefetch0(pkt21);
1554
1555         pkt10 = pkts[2];
1556         pkt11 = pkts[3];
1557         rte_prefetch0(pkt10);
1558         rte_prefetch0(pkt11);
1559
1560         q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1561         q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1562
1563         pkt00 = pkts[4];
1564         pkt01 = pkts[5];
1565         rte_prefetch0(pkt00);
1566         rte_prefetch0(pkt01);
1567
1568         q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1569         q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1570
1571         q20_base = rte_sched_port_qbase(port, q20);
1572         q21_base = rte_sched_port_qbase(port, q21);
1573         rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1574         rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1575
1576         /* Run the pipeline */
1577         for (i = 6; i < (n_pkts & (~1)); i += 2) {
1578                 /* Propagate stage inputs */
1579                 pkt30 = pkt20;
1580                 pkt31 = pkt21;
1581                 pkt20 = pkt10;
1582                 pkt21 = pkt11;
1583                 pkt10 = pkt00;
1584                 pkt11 = pkt01;
1585                 q30 = q20;
1586                 q31 = q21;
1587                 q20 = q10;
1588                 q21 = q11;
1589                 q30_base = q20_base;
1590                 q31_base = q21_base;
1591
1592                 /* Stage 0: Get packets in */
1593                 pkt00 = pkts[i];
1594                 pkt01 = pkts[i + 1];
1595                 rte_prefetch0(pkt00);
1596                 rte_prefetch0(pkt01);
1597
1598                 /* Stage 1: Prefetch queue structure storing queue pointers */
1599                 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1600                 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1601
1602                 /* Stage 2: Prefetch queue write location */
1603                 q20_base = rte_sched_port_qbase(port, q20);
1604                 q21_base = rte_sched_port_qbase(port, q21);
1605                 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1606                 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1607
1608                 /* Stage 3: Write packet to queue and activate queue */
1609                 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1610                 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1611                 result += r30 + r31;
1612         }
1613
1614         /*
1615          * Drain the pipeline (exactly 6 packets).
1616          * Handle the last packet in the case
1617          * of an odd number of input packets.
1618          */
1619         pkt_last = pkts[n_pkts - 1];
1620         rte_prefetch0(pkt_last);
1621
1622         q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1623         q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1624
1625         q10_base = rte_sched_port_qbase(port, q10);
1626         q11_base = rte_sched_port_qbase(port, q11);
1627         rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1628         rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1629
1630         r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1631         r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1632         result += r20 + r21;
1633
1634         q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1635
1636         q00_base = rte_sched_port_qbase(port, q00);
1637         q01_base = rte_sched_port_qbase(port, q01);
1638         rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1639         rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1640
1641         r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1642         r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1643         result += r10 + r11;
1644
1645         q_last_base = rte_sched_port_qbase(port, q_last);
1646         rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1647
1648         r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1649         r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1650         result += r00 + r01;
1651
1652         if (n_pkts & 1) {
1653                 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1654                 result += r_last;
1655         }
1656
1657         return result;
1658 }
1659
1660 #ifndef RTE_SCHED_SUBPORT_TC_OV
1661
1662 static inline void
1663 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1664 {
1665         struct rte_sched_grinder *grinder = port->grinder + pos;
1666         struct rte_sched_subport *subport = grinder->subport;
1667         struct rte_sched_pipe *pipe = grinder->pipe;
1668         struct rte_sched_pipe_profile *params = grinder->pipe_params;
1669         uint64_t n_periods;
1670         uint32_t i;
1671
1672         /* Subport TB */
1673         n_periods = (port->time - subport->tb_time) / subport->tb_period;
1674         subport->tb_credits += n_periods * subport->tb_credits_per_period;
1675         subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1676         subport->tb_time += n_periods * subport->tb_period;
1677
1678         /* Pipe TB */
1679         n_periods = (port->time - pipe->tb_time) / params->tb_period;
1680         pipe->tb_credits += n_periods * params->tb_credits_per_period;
1681         pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1682         pipe->tb_time += n_periods * params->tb_period;
1683
1684         /* Subport TCs */
1685         if (unlikely(port->time >= subport->tc_time)) {
1686                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1687                         subport->tc_credits[i] = subport->tc_credits_per_period[i];
1688
1689                 subport->tc_time = port->time + subport->tc_period;
1690         }
1691
1692         /* Pipe TCs */
1693         if (unlikely(port->time >= pipe->tc_time)) {
1694                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1695                         pipe->tc_credits[i] = params->tc_credits_per_period[i];
1696
1697                 pipe->tc_time = port->time + params->tc_period;
1698         }
1699 }
1700
1701 #else
1702
1703 static inline uint32_t
1704 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1705 {
1706         struct rte_sched_grinder *grinder = port->grinder + pos;
1707         struct rte_sched_subport *subport = grinder->subport;
1708         uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1709         uint32_t tc_consumption = 0, tc_ov_consumption_max;
1710         uint32_t tc_ov_wm = subport->tc_ov_wm;
1711         uint32_t i;
1712
1713         if (subport->tc_ov == 0)
1714                 return subport->tc_ov_wm_max;
1715
1716         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
1717                 tc_ov_consumption[i] =
1718                         subport->tc_credits_per_period[i] - subport->tc_credits[i];
1719                 tc_consumption += tc_ov_consumption[i];
1720         }
1721
1722         tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
1723                 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
1724                 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
1725
1726         tc_ov_consumption_max =
1727                 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
1728                         tc_consumption;
1729
1730         if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
1731                 (tc_ov_consumption_max - port->mtu)) {
1732                 tc_ov_wm  -= tc_ov_wm >> 7;
1733                 if (tc_ov_wm < subport->tc_ov_wm_min)
1734                         tc_ov_wm = subport->tc_ov_wm_min;
1735
1736                 return tc_ov_wm;
1737         }
1738
1739         tc_ov_wm += (tc_ov_wm >> 7) + 1;
1740         if (tc_ov_wm > subport->tc_ov_wm_max)
1741                 tc_ov_wm = subport->tc_ov_wm_max;
1742
1743         return tc_ov_wm;
1744 }
1745
1746 static inline void
1747 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1748 {
1749         struct rte_sched_grinder *grinder = port->grinder + pos;
1750         struct rte_sched_subport *subport = grinder->subport;
1751         struct rte_sched_pipe *pipe = grinder->pipe;
1752         struct rte_sched_pipe_profile *params = grinder->pipe_params;
1753         uint64_t n_periods;
1754         uint32_t i;
1755
1756         /* Subport TB */
1757         n_periods = (port->time - subport->tb_time) / subport->tb_period;
1758         subport->tb_credits += n_periods * subport->tb_credits_per_period;
1759         subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1760         subport->tb_time += n_periods * subport->tb_period;
1761
1762         /* Pipe TB */
1763         n_periods = (port->time - pipe->tb_time) / params->tb_period;
1764         pipe->tb_credits += n_periods * params->tb_credits_per_period;
1765         pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1766         pipe->tb_time += n_periods * params->tb_period;
1767
1768         /* Subport TCs */
1769         if (unlikely(port->time >= subport->tc_time)) {
1770                 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
1771
1772                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1773                         subport->tc_credits[i] = subport->tc_credits_per_period[i];
1774
1775                 subport->tc_time = port->time + subport->tc_period;
1776                 subport->tc_ov_period_id++;
1777         }
1778
1779         /* Pipe TCs */
1780         if (unlikely(port->time >= pipe->tc_time)) {
1781                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1782                         pipe->tc_credits[i] = params->tc_credits_per_period[i];
1783                 pipe->tc_time = port->time + params->tc_period;
1784         }
1785
1786         /* Pipe TCs - Oversubscription */
1787         if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
1788                 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
1789
1790                 pipe->tc_ov_period_id = subport->tc_ov_period_id;
1791         }
1792 }
1793
1794 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
1795
1796
1797 #ifndef RTE_SCHED_SUBPORT_TC_OV
1798
1799 static inline int
1800 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1801 {
1802         struct rte_sched_grinder *grinder = port->grinder + pos;
1803         struct rte_sched_subport *subport = grinder->subport;
1804         struct rte_sched_pipe *pipe = grinder->pipe;
1805         struct rte_mbuf *pkt = grinder->pkt;
1806         uint32_t tc_index = grinder->tc_index;
1807         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1808         uint32_t subport_tb_credits = subport->tb_credits;
1809         uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1810         uint32_t pipe_tb_credits = pipe->tb_credits;
1811         uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1812         int enough_credits;
1813
1814         /* Check queue credits */
1815         enough_credits = (pkt_len <= subport_tb_credits) &&
1816                 (pkt_len <= subport_tc_credits) &&
1817                 (pkt_len <= pipe_tb_credits) &&
1818                 (pkt_len <= pipe_tc_credits);
1819
1820         if (!enough_credits)
1821                 return 0;
1822
1823         /* Update port credits */
1824         subport->tb_credits -= pkt_len;
1825         subport->tc_credits[tc_index] -= pkt_len;
1826         pipe->tb_credits -= pkt_len;
1827         pipe->tc_credits[tc_index] -= pkt_len;
1828
1829         return 1;
1830 }
1831
1832 #else
1833
1834 static inline int
1835 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1836 {
1837         struct rte_sched_grinder *grinder = port->grinder + pos;
1838         struct rte_sched_subport *subport = grinder->subport;
1839         struct rte_sched_pipe *pipe = grinder->pipe;
1840         struct rte_mbuf *pkt = grinder->pkt;
1841         uint32_t tc_index = grinder->tc_index;
1842         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1843         uint32_t subport_tb_credits = subport->tb_credits;
1844         uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1845         uint32_t pipe_tb_credits = pipe->tb_credits;
1846         uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1847         uint32_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1848         uint32_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
1849         uint32_t pipe_tc_ov_credits, i;
1850         int enough_credits;
1851
1852         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1853                 pipe_tc_ov_mask1[i] = UINT32_MAX;
1854
1855         pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
1856         pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = UINT32_MAX;
1857         pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
1858
1859         /* Check pipe and subport credits */
1860         enough_credits = (pkt_len <= subport_tb_credits) &&
1861                 (pkt_len <= subport_tc_credits) &&
1862                 (pkt_len <= pipe_tb_credits) &&
1863                 (pkt_len <= pipe_tc_credits) &&
1864                 (pkt_len <= pipe_tc_ov_credits);
1865
1866         if (!enough_credits)
1867                 return 0;
1868
1869         /* Update pipe and subport credits */
1870         subport->tb_credits -= pkt_len;
1871         subport->tc_credits[tc_index] -= pkt_len;
1872         pipe->tb_credits -= pkt_len;
1873         pipe->tc_credits[tc_index] -= pkt_len;
1874         pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
1875
1876         return 1;
1877 }
1878
1879 #endif /* RTE_SCHED_SUBPORT_TC_OV */
1880
1881
1882 static inline int
1883 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
1884 {
1885         struct rte_sched_grinder *grinder = port->grinder + pos;
1886         struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
1887         struct rte_mbuf *pkt = grinder->pkt;
1888         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1889         uint32_t be_tc_active;
1890
1891         if (!grinder_credits_check(port, pos))
1892                 return 0;
1893
1894         /* Advance port time */
1895         port->time += pkt_len;
1896
1897         /* Send packet */
1898         port->pkts_out[port->n_pkts_out++] = pkt;
1899         queue->qr++;
1900
1901         be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
1902         grinder->wrr_tokens[grinder->qpos] +=
1903                 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
1904
1905         if (queue->qr == queue->qw) {
1906                 uint32_t qindex = grinder->qindex[grinder->qpos];
1907
1908                 rte_bitmap_clear(port->bmp, qindex);
1909                 grinder->qmask &= ~(1 << grinder->qpos);
1910                 if (be_tc_active)
1911                         grinder->wrr_mask[grinder->qpos] = 0;
1912                 rte_sched_port_set_queue_empty_timestamp(port, qindex);
1913         }
1914
1915         /* Reset pipe loop detection */
1916         port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1917         grinder->productive = 1;
1918
1919         return 1;
1920 }
1921
1922 #ifdef SCHED_VECTOR_SSE4
1923
1924 static inline int
1925 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1926 {
1927         __m128i index = _mm_set1_epi32(base_pipe);
1928         __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
1929         __m128i res = _mm_cmpeq_epi32(pipes, index);
1930
1931         pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
1932         pipes = _mm_cmpeq_epi32(pipes, index);
1933         res = _mm_or_si128(res, pipes);
1934
1935         if (_mm_testz_si128(res, res))
1936                 return 0;
1937
1938         return 1;
1939 }
1940
1941 #elif defined(SCHED_VECTOR_NEON)
1942
1943 static inline int
1944 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1945 {
1946         uint32x4_t index, pipes;
1947         uint32_t *pos = (uint32_t *)port->grinder_base_bmp_pos;
1948
1949         index = vmovq_n_u32(base_pipe);
1950         pipes = vld1q_u32(pos);
1951         if (!vminvq_u32(veorq_u32(pipes, index)))
1952                 return 1;
1953
1954         pipes = vld1q_u32(pos + 4);
1955         if (!vminvq_u32(veorq_u32(pipes, index)))
1956                 return 1;
1957
1958         return 0;
1959 }
1960
1961 #else
1962
1963 static inline int
1964 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1965 {
1966         uint32_t i;
1967
1968         for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
1969                 if (port->grinder_base_bmp_pos[i] == base_pipe)
1970                         return 1;
1971         }
1972
1973         return 0;
1974 }
1975
1976 #endif /* RTE_SCHED_OPTIMIZATIONS */
1977
1978 static inline void
1979 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
1980 {
1981         struct rte_sched_grinder *grinder = port->grinder + pos;
1982         uint16_t w[4];
1983
1984         grinder->pcache_w = 0;
1985         grinder->pcache_r = 0;
1986
1987         w[0] = (uint16_t) bmp_slab;
1988         w[1] = (uint16_t) (bmp_slab >> 16);
1989         w[2] = (uint16_t) (bmp_slab >> 32);
1990         w[3] = (uint16_t) (bmp_slab >> 48);
1991
1992         grinder->pcache_qmask[grinder->pcache_w] = w[0];
1993         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
1994         grinder->pcache_w += (w[0] != 0);
1995
1996         grinder->pcache_qmask[grinder->pcache_w] = w[1];
1997         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
1998         grinder->pcache_w += (w[1] != 0);
1999
2000         grinder->pcache_qmask[grinder->pcache_w] = w[2];
2001         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2002         grinder->pcache_w += (w[2] != 0);
2003
2004         grinder->pcache_qmask[grinder->pcache_w] = w[3];
2005         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2006         grinder->pcache_w += (w[3] != 0);
2007 }
2008
2009 static inline void
2010 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
2011 {
2012         struct rte_sched_grinder *grinder = port->grinder + pos;
2013         uint8_t b, i;
2014
2015         grinder->tccache_w = 0;
2016         grinder->tccache_r = 0;
2017
2018         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2019                 b = (uint8_t) ((qmask >> i) & 0x1);
2020                 grinder->tccache_qmask[grinder->tccache_w] = b;
2021                 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2022                 grinder->tccache_w += (b != 0);
2023         }
2024
2025         b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2026         grinder->tccache_qmask[grinder->tccache_w] = b;
2027         grinder->tccache_qindex[grinder->tccache_w] = qindex +
2028                 RTE_SCHED_TRAFFIC_CLASS_BE;
2029         grinder->tccache_w += (b != 0);
2030 }
2031
2032 static inline int
2033 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
2034 {
2035         struct rte_sched_grinder *grinder = port->grinder + pos;
2036         struct rte_mbuf **qbase;
2037         uint32_t qindex;
2038         uint16_t qsize;
2039
2040         if (grinder->tccache_r == grinder->tccache_w)
2041                 return 0;
2042
2043         qindex = grinder->tccache_qindex[grinder->tccache_r];
2044         qbase = rte_sched_port_qbase(port, qindex);
2045         qsize = rte_sched_port_qsize(port, qindex);
2046
2047         grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2048         grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2049         grinder->qsize = qsize;
2050
2051         if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2052                 grinder->queue[0] = port->queue + qindex;
2053                 grinder->qbase[0] = qbase;
2054                 grinder->qindex[0] = qindex;
2055                 grinder->tccache_r++;
2056
2057                 return 1;
2058         }
2059
2060         grinder->queue[0] = port->queue + qindex;
2061         grinder->queue[1] = port->queue + qindex + 1;
2062         grinder->queue[2] = port->queue + qindex + 2;
2063         grinder->queue[3] = port->queue + qindex + 3;
2064
2065         grinder->qbase[0] = qbase;
2066         grinder->qbase[1] = qbase + qsize;
2067         grinder->qbase[2] = qbase + 2 * qsize;
2068         grinder->qbase[3] = qbase + 3 * qsize;
2069
2070         grinder->qindex[0] = qindex;
2071         grinder->qindex[1] = qindex + 1;
2072         grinder->qindex[2] = qindex + 2;
2073         grinder->qindex[3] = qindex + 3;
2074
2075         grinder->tccache_r++;
2076         return 1;
2077 }
2078
2079 static inline int
2080 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
2081 {
2082         struct rte_sched_grinder *grinder = port->grinder + pos;
2083         uint32_t pipe_qindex;
2084         uint16_t pipe_qmask;
2085
2086         if (grinder->pcache_r < grinder->pcache_w) {
2087                 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2088                 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2089                 grinder->pcache_r++;
2090         } else {
2091                 uint64_t bmp_slab = 0;
2092                 uint32_t bmp_pos = 0;
2093
2094                 /* Get another non-empty pipe group */
2095                 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
2096                         return 0;
2097
2098 #ifdef RTE_SCHED_DEBUG
2099                 debug_check_queue_slab(port, bmp_pos, bmp_slab);
2100 #endif
2101
2102                 /* Return if pipe group already in one of the other grinders */
2103                 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2104                 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
2105                         return 0;
2106
2107                 port->grinder_base_bmp_pos[pos] = bmp_pos;
2108
2109                 /* Install new pipe group into grinder's pipe cache */
2110                 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
2111
2112                 pipe_qmask = grinder->pcache_qmask[0];
2113                 pipe_qindex = grinder->pcache_qindex[0];
2114                 grinder->pcache_r = 1;
2115         }
2116
2117         /* Install new pipe in the grinder */
2118         grinder->pindex = pipe_qindex >> 4;
2119         grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
2120         grinder->pipe = port->pipe + grinder->pindex;
2121         grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2122         grinder->productive = 0;
2123
2124         grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
2125         grinder_next_tc(port, pos);
2126
2127         /* Check for pipe exhaustion */
2128         if (grinder->pindex == port->pipe_loop) {
2129                 port->pipe_exhaustion = 1;
2130                 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2131         }
2132
2133         return 1;
2134 }
2135
2136
2137 static inline void
2138 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
2139 {
2140         struct rte_sched_grinder *grinder = port->grinder + pos;
2141         struct rte_sched_pipe *pipe = grinder->pipe;
2142         struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2143         uint32_t qmask = grinder->qmask;
2144
2145         grinder->wrr_tokens[0] =
2146                 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2147         grinder->wrr_tokens[1] =
2148                 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2149         grinder->wrr_tokens[2] =
2150                 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2151         grinder->wrr_tokens[3] =
2152                 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2153
2154         grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2155         grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2156         grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2157         grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2158
2159         grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2160         grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2161         grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2162         grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2163 }
2164
2165 static inline void
2166 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
2167 {
2168         struct rte_sched_grinder *grinder = port->grinder + pos;
2169         struct rte_sched_pipe *pipe = grinder->pipe;
2170
2171         pipe->wrr_tokens[0] =
2172                         (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2173                                 RTE_SCHED_WRR_SHIFT;
2174         pipe->wrr_tokens[1] =
2175                         (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2176                                 RTE_SCHED_WRR_SHIFT;
2177         pipe->wrr_tokens[2] =
2178                         (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2179                                 RTE_SCHED_WRR_SHIFT;
2180         pipe->wrr_tokens[3] =
2181                         (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2182                                 RTE_SCHED_WRR_SHIFT;
2183 }
2184
2185 static inline void
2186 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
2187 {
2188         struct rte_sched_grinder *grinder = port->grinder + pos;
2189         uint16_t wrr_tokens_min;
2190
2191         grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2192         grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2193         grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2194         grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2195
2196         grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2197         wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2198
2199         grinder->wrr_tokens[0] -= wrr_tokens_min;
2200         grinder->wrr_tokens[1] -= wrr_tokens_min;
2201         grinder->wrr_tokens[2] -= wrr_tokens_min;
2202         grinder->wrr_tokens[3] -= wrr_tokens_min;
2203 }
2204
2205
2206 #define grinder_evict(port, pos)
2207
2208 static inline void
2209 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
2210 {
2211         struct rte_sched_grinder *grinder = port->grinder + pos;
2212
2213         rte_prefetch0(grinder->pipe);
2214         rte_prefetch0(grinder->queue[0]);
2215 }
2216
2217 static inline void
2218 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
2219 {
2220         struct rte_sched_grinder *grinder = port->grinder + pos;
2221         uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2222
2223         qsize = grinder->qsize;
2224         grinder->qpos = 0;
2225
2226         if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2227                 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2228
2229                 rte_prefetch0(grinder->qbase[0] + qr[0]);
2230                 return;
2231         }
2232
2233         qr[0] = grinder->queue[0]->qr & (qsize - 1);
2234         qr[1] = grinder->queue[1]->qr & (qsize - 1);
2235         qr[2] = grinder->queue[2]->qr & (qsize - 1);
2236         qr[3] = grinder->queue[3]->qr & (qsize - 1);
2237
2238         rte_prefetch0(grinder->qbase[0] + qr[0]);
2239         rte_prefetch0(grinder->qbase[1] + qr[1]);
2240
2241         grinder_wrr_load(port, pos);
2242         grinder_wrr(port, pos);
2243
2244         rte_prefetch0(grinder->qbase[2] + qr[2]);
2245         rte_prefetch0(grinder->qbase[3] + qr[3]);
2246 }
2247
2248 static inline void
2249 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2250 {
2251         struct rte_sched_grinder *grinder = port->grinder + pos;
2252         uint32_t qpos = grinder->qpos;
2253         struct rte_mbuf **qbase = grinder->qbase[qpos];
2254         uint16_t qsize = grinder->qsize;
2255         uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2256
2257         grinder->pkt = qbase[qr];
2258         rte_prefetch0(grinder->pkt);
2259
2260         if (unlikely((qr & 0x7) == 7)) {
2261                 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2262
2263                 rte_prefetch0(qbase + qr_next);
2264         }
2265 }
2266
2267 static inline uint32_t
2268 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2269 {
2270         struct rte_sched_grinder *grinder = port->grinder + pos;
2271
2272         switch (grinder->state) {
2273         case e_GRINDER_PREFETCH_PIPE:
2274         {
2275                 if (grinder_next_pipe(port, pos)) {
2276                         grinder_prefetch_pipe(port, pos);
2277                         port->busy_grinders++;
2278
2279                         grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2280                         return 0;
2281                 }
2282
2283                 return 0;
2284         }
2285
2286         case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2287         {
2288                 struct rte_sched_pipe *pipe = grinder->pipe;
2289
2290                 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2291                 grinder_prefetch_tc_queue_arrays(port, pos);
2292                 grinder_credits_update(port, pos);
2293
2294                 grinder->state = e_GRINDER_PREFETCH_MBUF;
2295                 return 0;
2296         }
2297
2298         case e_GRINDER_PREFETCH_MBUF:
2299         {
2300                 grinder_prefetch_mbuf(port, pos);
2301
2302                 grinder->state = e_GRINDER_READ_MBUF;
2303                 return 0;
2304         }
2305
2306         case e_GRINDER_READ_MBUF:
2307         {
2308                 uint32_t wrr_active, result = 0;
2309
2310                 result = grinder_schedule(port, pos);
2311
2312                 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2313
2314                 /* Look for next packet within the same TC */
2315                 if (result && grinder->qmask) {
2316                         if (wrr_active)
2317                                 grinder_wrr(port, pos);
2318
2319                         grinder_prefetch_mbuf(port, pos);
2320
2321                         return 1;
2322                 }
2323
2324                 if (wrr_active)
2325                         grinder_wrr_store(port, pos);
2326
2327                 /* Look for another active TC within same pipe */
2328                 if (grinder_next_tc(port, pos)) {
2329                         grinder_prefetch_tc_queue_arrays(port, pos);
2330
2331                         grinder->state = e_GRINDER_PREFETCH_MBUF;
2332                         return result;
2333                 }
2334
2335                 if (grinder->productive == 0 &&
2336                     port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2337                         port->pipe_loop = grinder->pindex;
2338
2339                 grinder_evict(port, pos);
2340
2341                 /* Look for another active pipe */
2342                 if (grinder_next_pipe(port, pos)) {
2343                         grinder_prefetch_pipe(port, pos);
2344
2345                         grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2346                         return result;
2347                 }
2348
2349                 /* No active pipe found */
2350                 port->busy_grinders--;
2351
2352                 grinder->state = e_GRINDER_PREFETCH_PIPE;
2353                 return result;
2354         }
2355
2356         default:
2357                 rte_panic("Algorithmic error (invalid state)\n");
2358                 return 0;
2359         }
2360 }
2361
2362 static inline void
2363 rte_sched_port_time_resync(struct rte_sched_port *port)
2364 {
2365         uint64_t cycles = rte_get_tsc_cycles();
2366         uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2367         uint64_t bytes_diff;
2368
2369         /* Compute elapsed time in bytes */
2370         bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2371                                            port->inv_cycles_per_byte);
2372
2373         /* Advance port time */
2374         port->time_cpu_cycles = cycles;
2375         port->time_cpu_bytes += bytes_diff;
2376         if (port->time < port->time_cpu_bytes)
2377                 port->time = port->time_cpu_bytes;
2378
2379         /* Reset pipe loop detection */
2380         port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2381 }
2382
2383 static inline int
2384 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2385 {
2386         int exceptions;
2387
2388         /* Check if any exception flag is set */
2389         exceptions = (second_pass && port->busy_grinders == 0) ||
2390                 (port->pipe_exhaustion == 1);
2391
2392         /* Clear exception flags */
2393         port->pipe_exhaustion = 0;
2394
2395         return exceptions;
2396 }
2397
2398 int
2399 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2400 {
2401         uint32_t i, count;
2402
2403         port->pkts_out = pkts;
2404         port->n_pkts_out = 0;
2405
2406         rte_sched_port_time_resync(port);
2407
2408         /* Take each queue in the grinder one step further */
2409         for (i = 0, count = 0; ; i++)  {
2410                 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2411                 if ((count == n_pkts) ||
2412                     rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {
2413                         break;
2414                 }
2415         }
2416
2417         return count;
2418 }