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