sched: add pipe config to subport 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 static uint32_t
554 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
555         enum rte_sched_subport_array array)
556 {
557         uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
558         uint32_t n_subport_pipe_queues =
559                 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
560
561         uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
562         uint32_t size_queue =
563                 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
564         uint32_t size_queue_extra
565                 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
566         uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
567                 sizeof(struct rte_sched_pipe_profile);
568         uint32_t size_bmp_array =
569                 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
570         uint32_t size_per_pipe_queue_array, size_queue_array;
571
572         uint32_t base, i;
573
574         size_per_pipe_queue_array = 0;
575         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
576                 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
577                         size_per_pipe_queue_array +=
578                                 params->qsize[i] * sizeof(struct rte_mbuf *);
579                 else
580                         size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
581                                 params->qsize[i] * sizeof(struct rte_mbuf *);
582         }
583         size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
584
585         base = 0;
586
587         if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
588                 return base;
589         base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
590
591         if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
592                 return base;
593         base += RTE_CACHE_LINE_ROUNDUP(size_queue);
594
595         if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
596                 return base;
597         base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
598
599         if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
600                 return base;
601         base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
602
603         if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
604                 return base;
605         base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
606
607         if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
608                 return base;
609         base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
610
611         return base;
612 }
613
614 static void
615 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
616 {
617         uint32_t i;
618
619         subport->qsize_add[0] = 0;
620
621         /* Strict prority traffic class */
622         for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
623                 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
624
625         /* Best-effort traffic class */
626         subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
627                 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
628                 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
629         subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
630                 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
631                 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
632         subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
633                 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
634                 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
635
636         subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
637                 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
638 }
639
640 static void
641 rte_sched_port_log_pipe_profile(struct rte_sched_port *port, uint32_t i)
642 {
643         struct rte_sched_pipe_profile *p = port->pipe_profiles + i;
644
645         RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
646                 "       Token bucket: period = %u, credits per period = %u, size = %u\n"
647                 "       Traffic classes: period = %u,\n"
648                 "       credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
649                 "       Best-effort traffic class oversubscription: weight = %hhu\n"
650                 "       WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
651                 i,
652
653                 /* Token bucket */
654                 p->tb_period,
655                 p->tb_credits_per_period,
656                 p->tb_size,
657
658                 /* Traffic classes */
659                 p->tc_period,
660                 p->tc_credits_per_period[0],
661                 p->tc_credits_per_period[1],
662                 p->tc_credits_per_period[2],
663                 p->tc_credits_per_period[3],
664                 p->tc_credits_per_period[4],
665                 p->tc_credits_per_period[5],
666                 p->tc_credits_per_period[6],
667                 p->tc_credits_per_period[7],
668                 p->tc_credits_per_period[8],
669                 p->tc_credits_per_period[9],
670                 p->tc_credits_per_period[10],
671                 p->tc_credits_per_period[11],
672                 p->tc_credits_per_period[12],
673
674                 /* Best-effort traffic class oversubscription */
675                 p->tc_ov_weight,
676
677                 /* WRR */
678                 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
679 }
680
681 static inline uint64_t
682 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
683 {
684         uint64_t time = time_ms;
685
686         time = (time * rate) / 1000;
687
688         return time;
689 }
690
691 static void
692 rte_sched_pipe_profile_convert(struct rte_sched_port *port,
693         struct rte_sched_pipe_params *src,
694         struct rte_sched_pipe_profile *dst,
695         uint32_t rate)
696 {
697         uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
698         uint32_t lcd1, lcd2, lcd;
699         uint32_t i;
700
701         /* Token Bucket */
702         if (src->tb_rate == rate) {
703                 dst->tb_credits_per_period = 1;
704                 dst->tb_period = 1;
705         } else {
706                 double tb_rate = (double) src->tb_rate
707                                 / (double) rate;
708                 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
709
710                 rte_approx(tb_rate, d,
711                         &dst->tb_credits_per_period, &dst->tb_period);
712         }
713
714         dst->tb_size = src->tb_size;
715
716         /* Traffic Classes */
717         dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
718                                                 rate);
719
720         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
721                 if (port->qsize[i])
722                         dst->tc_credits_per_period[i]
723                                 = rte_sched_time_ms_to_bytes(src->tc_period,
724                                         src->tc_rate[i]);
725
726         dst->tc_ov_weight = src->tc_ov_weight;
727
728         /* WRR queues */
729         wrr_cost[0] = src->wrr_weights[0];
730         wrr_cost[1] = src->wrr_weights[1];
731         wrr_cost[2] = src->wrr_weights[2];
732         wrr_cost[3] = src->wrr_weights[3];
733
734         lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
735         lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
736         lcd = rte_get_lcd(lcd1, lcd2);
737
738         wrr_cost[0] = lcd / wrr_cost[0];
739         wrr_cost[1] = lcd / wrr_cost[1];
740         wrr_cost[2] = lcd / wrr_cost[2];
741         wrr_cost[3] = lcd / wrr_cost[3];
742
743         dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
744         dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
745         dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
746         dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
747 }
748
749 static int
750 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
751         uint32_t n_max_pipes_per_subport,
752         uint32_t rate)
753 {
754         uint32_t i;
755
756         /* Check user parameters */
757         if (params == NULL) {
758                 RTE_LOG(ERR, SCHED,
759                         "%s: Incorrect value for parameter params\n", __func__);
760                 return -EINVAL;
761         }
762
763         if (params->tb_rate == 0 || params->tb_rate > rate) {
764                 RTE_LOG(ERR, SCHED,
765                         "%s: Incorrect value for tb rate\n", __func__);
766                 return -EINVAL;
767         }
768
769         if (params->tb_size == 0) {
770                 RTE_LOG(ERR, SCHED,
771                         "%s: Incorrect value for tb size\n", __func__);
772                 return -EINVAL;
773         }
774
775         /* qsize: if non-zero, power of 2,
776          * no bigger than 32K (due to 16-bit read/write pointers)
777          */
778         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
779                 uint16_t qsize = params->qsize[i];
780
781                 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
782                         RTE_LOG(ERR, SCHED,
783                                 "%s: Incorrect value for qsize\n", __func__);
784                         return -EINVAL;
785                 }
786         }
787
788         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
789                 uint32_t tc_rate = params->tc_rate[i];
790                 uint16_t qsize = params->qsize[i];
791
792                 if ((qsize == 0 && tc_rate != 0) ||
793                         (qsize != 0 && tc_rate == 0) ||
794                         (tc_rate > params->tb_rate)) {
795                         RTE_LOG(ERR, SCHED,
796                                 "%s: Incorrect value for tc rate\n", __func__);
797                         return -EINVAL;
798                 }
799         }
800
801         if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
802                 params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
803                 RTE_LOG(ERR, SCHED,
804                         "%s: Incorrect qsize or tc rate(best effort)\n", __func__);
805                 return -EINVAL;
806         }
807
808         if (params->tc_period == 0) {
809                 RTE_LOG(ERR, SCHED,
810                         "%s: Incorrect value for tc period\n", __func__);
811                 return -EINVAL;
812         }
813
814         /* n_pipes_per_subport: non-zero, power of 2 */
815         if (params->n_pipes_per_subport_enabled == 0 ||
816                 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
817             !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
818                 RTE_LOG(ERR, SCHED,
819                         "%s: Incorrect value for pipes number\n", __func__);
820                 return -EINVAL;
821         }
822
823         /* pipe_profiles and n_pipe_profiles */
824         if (params->pipe_profiles == NULL ||
825             params->n_pipe_profiles == 0 ||
826                 params->n_max_pipe_profiles == 0 ||
827                 params->n_pipe_profiles > params->n_max_pipe_profiles) {
828                 RTE_LOG(ERR, SCHED,
829                         "%s: Incorrect value for pipe profiles\n", __func__);
830                 return -EINVAL;
831         }
832
833         for (i = 0; i < params->n_pipe_profiles; i++) {
834                 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
835                 int status;
836
837                 status = pipe_profile_check(p, rate, &params->qsize[0]);
838                 if (status != 0) {
839                         RTE_LOG(ERR, SCHED,
840                                 "%s: Pipe profile check failed(%d)\n", __func__, status);
841                         return -EINVAL;
842                 }
843         }
844
845         return 0;
846 }
847
848 uint32_t
849 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
850 {
851         uint32_t size0, size1;
852         int status;
853
854         status = rte_sched_port_check_params(params);
855         if (status != 0) {
856                 RTE_LOG(NOTICE, SCHED,
857                         "Port scheduler params check failed (%d)\n", status);
858
859                 return 0;
860         }
861
862         size0 = sizeof(struct rte_sched_port);
863         size1 = rte_sched_port_get_array_base(params,
864                         e_RTE_SCHED_PORT_ARRAY_TOTAL);
865
866         return size0 + size1;
867 }
868
869 struct rte_sched_port *
870 rte_sched_port_config(struct rte_sched_port_params *params)
871 {
872         struct rte_sched_port *port = NULL;
873         uint32_t size0, size1;
874         uint32_t cycles_per_byte;
875         uint32_t i, j;
876         int status;
877
878         status = rte_sched_port_check_params(params);
879         if (status != 0) {
880                 RTE_LOG(ERR, SCHED,
881                         "%s: Port scheduler params check failed (%d)\n",
882                         __func__, status);
883                 return NULL;
884         }
885
886         size0 = sizeof(struct rte_sched_port);
887         size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
888
889         /* Allocate memory to store the data structures */
890         port = rte_zmalloc_socket("qos_params", size0 + size1, RTE_CACHE_LINE_SIZE,
891                 params->socket);
892         if (port == NULL) {
893                 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
894
895                 return NULL;
896         }
897
898         /* User parameters */
899         port->n_subports_per_port = params->n_subports_per_port;
900         port->n_pipes_per_subport = params->n_pipes_per_subport;
901         port->n_pipes_per_subport_log2 =
902                         __builtin_ctz(params->n_pipes_per_subport);
903         port->socket = params->socket;
904
905         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
906                 port->pipe_queue[i] = i;
907
908         for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
909                 port->pipe_tc[i] = j;
910
911                 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
912                         j++;
913         }
914
915         for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
916                 port->tc_queue[i] = j;
917
918                 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
919                         j++;
920         }
921         port->rate = params->rate;
922         port->mtu = params->mtu + params->frame_overhead;
923         port->frame_overhead = params->frame_overhead;
924
925         /* Timing */
926         port->time_cpu_cycles = rte_get_tsc_cycles();
927         port->time_cpu_bytes = 0;
928         port->time = 0;
929
930         cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
931                 / params->rate;
932         port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
933
934         /* Grinders */
935         port->pkts_out = NULL;
936         port->n_pkts_out = 0;
937
938         return port;
939 }
940
941 static inline void
942 rte_sched_subport_free(struct rte_sched_port *port,
943         struct rte_sched_subport *subport)
944 {
945         uint32_t n_subport_pipe_queues;
946         uint32_t qindex;
947
948         if (subport == NULL)
949                 return;
950
951         n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
952
953         /* Free enqueued mbufs */
954         for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
955                 struct rte_mbuf **mbufs =
956                         rte_sched_subport_pipe_qbase(subport, qindex);
957                 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
958                 if (qsize != 0) {
959                         struct rte_sched_queue *queue = subport->queue + qindex;
960                         uint16_t qr = queue->qr & (qsize - 1);
961                         uint16_t qw = queue->qw & (qsize - 1);
962
963                         for (; qr != qw; qr = (qr + 1) & (qsize - 1))
964                                 rte_pktmbuf_free(mbufs[qr]);
965                 }
966         }
967
968         rte_bitmap_free(subport->bmp);
969 }
970
971 void
972 rte_sched_port_free(struct rte_sched_port *port)
973 {
974         uint32_t i;
975
976         /* Check user parameters */
977         if (port == NULL)
978                 return;
979
980         for (i = 0; i < port->n_subports_per_port; i++)
981                 rte_sched_subport_free(port, port->subports[i]);
982
983         rte_free(port);
984 }
985
986 static void
987 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
988 {
989         struct rte_sched_subport *s = port->subports[i];
990
991         RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
992                 "       Token bucket: period = %u, credits per period = %u, size = %u\n"
993                 "       Traffic classes: period = %u\n"
994                 "       credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
995                 "       Best effort traffic class oversubscription: wm min = %u, wm max = %u\n",
996                 i,
997
998                 /* Token bucket */
999                 s->tb_period,
1000                 s->tb_credits_per_period,
1001                 s->tb_size,
1002
1003                 /* Traffic classes */
1004                 s->tc_period,
1005                 s->tc_credits_per_period[0],
1006                 s->tc_credits_per_period[1],
1007                 s->tc_credits_per_period[2],
1008                 s->tc_credits_per_period[3],
1009                 s->tc_credits_per_period[4],
1010                 s->tc_credits_per_period[5],
1011                 s->tc_credits_per_period[6],
1012                 s->tc_credits_per_period[7],
1013                 s->tc_credits_per_period[8],
1014                 s->tc_credits_per_period[9],
1015                 s->tc_credits_per_period[10],
1016                 s->tc_credits_per_period[11],
1017                 s->tc_credits_per_period[12],
1018
1019                 /* Best effort traffic class oversubscription */
1020                 s->tc_ov_wm_min,
1021                 s->tc_ov_wm_max);
1022 }
1023
1024 static void
1025 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1026 {
1027         uint32_t i;
1028
1029         for (i = 0; i < n_subports; i++) {
1030                 struct rte_sched_subport *subport = port->subports[i];
1031
1032                 rte_sched_subport_free(port, subport);
1033         }
1034
1035         rte_free(port);
1036 }
1037
1038 int
1039 rte_sched_subport_config(struct rte_sched_port *port,
1040         uint32_t subport_id,
1041         struct rte_sched_subport_params *params)
1042 {
1043         struct rte_sched_subport *s = NULL;
1044         uint32_t n_subports = subport_id;
1045         uint32_t n_subport_pipe_queues, i;
1046         uint32_t size0, size1, bmp_mem_size;
1047         int status;
1048
1049         /* Check user parameters */
1050         if (port == NULL) {
1051                 RTE_LOG(ERR, SCHED,
1052                         "%s: Incorrect value for parameter port\n", __func__);
1053                 return 0;
1054         }
1055
1056         if (subport_id >= port->n_subports_per_port) {
1057                 RTE_LOG(ERR, SCHED,
1058                         "%s: Incorrect value for subport id\n", __func__);
1059
1060                 rte_sched_free_memory(port, n_subports);
1061                 return -EINVAL;
1062         }
1063
1064         status = rte_sched_subport_check_params(params,
1065                 port->n_pipes_per_subport,
1066                 port->rate);
1067         if (status != 0) {
1068                 RTE_LOG(NOTICE, SCHED,
1069                         "%s: Port scheduler params check failed (%d)\n",
1070                         __func__, status);
1071
1072                 rte_sched_free_memory(port, n_subports);
1073                 return -EINVAL;
1074         }
1075
1076         /* Determine the amount of memory to allocate */
1077         size0 = sizeof(struct rte_sched_subport);
1078         size1 = rte_sched_subport_get_array_base(params,
1079                                 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1080
1081         /* Allocate memory to store the data structures */
1082         s = rte_zmalloc_socket("subport_params", size0 + size1,
1083                 RTE_CACHE_LINE_SIZE, port->socket);
1084         if (s == NULL) {
1085                 RTE_LOG(ERR, SCHED,
1086                         "%s: Memory allocation fails\n", __func__);
1087
1088                 rte_sched_free_memory(port, n_subports);
1089                 return -ENOMEM;
1090         }
1091
1092         n_subports++;
1093
1094         /* Port */
1095         port->subports[subport_id] = s;
1096
1097         /* Token Bucket (TB) */
1098         if (params->tb_rate == port->rate) {
1099                 s->tb_credits_per_period = 1;
1100                 s->tb_period = 1;
1101         } else {
1102                 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
1103                 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
1104
1105                 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
1106         }
1107
1108         s->tb_size = params->tb_size;
1109         s->tb_time = port->time;
1110         s->tb_credits = s->tb_size / 2;
1111
1112         /* Traffic Classes (TCs) */
1113         s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
1114         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1115                 if (params->qsize[i])
1116                         s->tc_credits_per_period[i]
1117                                 = rte_sched_time_ms_to_bytes(params->tc_period,
1118                                         params->tc_rate[i]);
1119         }
1120         s->tc_time = port->time + s->tc_period;
1121         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1122                 if (params->qsize[i])
1123                         s->tc_credits[i] = s->tc_credits_per_period[i];
1124
1125         /* compile time checks */
1126         RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1127         RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1128                 (RTE_SCHED_PORT_N_GRINDERS - 1));
1129
1130         /* User parameters */
1131         s->n_pipes_per_subport_enabled = params->n_pipes_per_subport_enabled;
1132         memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1133         s->n_pipe_profiles = params->n_pipe_profiles;
1134         s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1135
1136 #ifdef RTE_SCHED_RED
1137         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1138                 uint32_t j;
1139
1140                 for (j = 0; j < RTE_COLORS; j++) {
1141                         /* if min/max are both zero, then RED is disabled */
1142                         if ((params->red_params[i][j].min_th |
1143                              params->red_params[i][j].max_th) == 0) {
1144                                 continue;
1145                         }
1146
1147                         if (rte_red_config_init(&s->red_config[i][j],
1148                                 params->red_params[i][j].wq_log2,
1149                                 params->red_params[i][j].min_th,
1150                                 params->red_params[i][j].max_th,
1151                                 params->red_params[i][j].maxp_inv) != 0) {
1152                                 rte_sched_free_memory(port, n_subports);
1153
1154                                 RTE_LOG(NOTICE, SCHED,
1155                                 "%s: RED configuration init fails\n", __func__);
1156                                 return -EINVAL;
1157                         }
1158                 }
1159         }
1160 #endif
1161
1162         /* Scheduling loop detection */
1163         s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1164         s->pipe_exhaustion = 0;
1165
1166         /* Grinders */
1167         s->busy_grinders = 0;
1168
1169         /* Queue base calculation */
1170         rte_sched_subport_config_qsize(s);
1171
1172         /* Large data structures */
1173         s->pipe = (struct rte_sched_pipe *)
1174                 (s->memory + rte_sched_subport_get_array_base(params,
1175                 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1176         s->queue = (struct rte_sched_queue *)
1177                 (s->memory + rte_sched_subport_get_array_base(params,
1178                 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1179         s->queue_extra = (struct rte_sched_queue_extra *)
1180                 (s->memory + rte_sched_subport_get_array_base(params,
1181                 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1182         s->pipe_profiles = (struct rte_sched_pipe_profile *)
1183                 (s->memory + rte_sched_subport_get_array_base(params,
1184                 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1185         s->bmp_array =  s->memory + rte_sched_subport_get_array_base(params,
1186                 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1187         s->queue_array = (struct rte_mbuf **)
1188                 (s->memory + rte_sched_subport_get_array_base(params,
1189                 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1190
1191         /* Bitmap */
1192         n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1193         bmp_mem_size = rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
1194         s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1195                                 bmp_mem_size);
1196         if (s->bmp == NULL) {
1197                 RTE_LOG(ERR, SCHED,
1198                         "%s: Subport bitmap init error\n", __func__);
1199
1200                 rte_sched_free_memory(port, n_subports);
1201                 return -EINVAL;
1202         }
1203
1204         for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1205                 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1206
1207 #ifdef RTE_SCHED_SUBPORT_TC_OV
1208         /* TC oversubscription */
1209         s->tc_ov_wm_min = port->mtu;
1210         s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
1211                                                      s->pipe_tc_be_rate_max);
1212         s->tc_ov_wm = s->tc_ov_wm_max;
1213         s->tc_ov_period_id = 0;
1214         s->tc_ov = 0;
1215         s->tc_ov_n = 0;
1216         s->tc_ov_rate = 0;
1217 #endif
1218
1219         rte_sched_port_log_subport_config(port, subport_id);
1220
1221         return 0;
1222 }
1223
1224 int
1225 rte_sched_pipe_config(struct rte_sched_port *port,
1226         uint32_t subport_id,
1227         uint32_t pipe_id,
1228         int32_t pipe_profile)
1229 {
1230         struct rte_sched_subport *s;
1231         struct rte_sched_pipe *p;
1232         struct rte_sched_pipe_profile *params;
1233         uint32_t deactivate, profile, i;
1234
1235         /* Check user parameters */
1236         profile = (uint32_t) pipe_profile;
1237         deactivate = (pipe_profile < 0);
1238
1239         if (port == NULL) {
1240                 RTE_LOG(ERR, SCHED,
1241                         "%s: Incorrect value for parameter port\n", __func__);
1242                 return -EINVAL;
1243         }
1244
1245         if (subport_id >= port->n_subports_per_port) {
1246                 RTE_LOG(ERR, SCHED,
1247                         "%s: Incorrect value for parameter subport id\n", __func__);
1248                 return -EINVAL;
1249         }
1250
1251         if (pipe_id >= port->n_pipes_per_subport) {
1252                 RTE_LOG(ERR, SCHED,
1253                         "%s: Incorrect value for parameter pipe id\n", __func__);
1254                 return -EINVAL;
1255         }
1256
1257         if (!deactivate && profile >= port->n_pipe_profiles) {
1258                 RTE_LOG(ERR, SCHED,
1259                         "%s: Incorrect value for parameter pipe profile\n", __func__);
1260                 return -EINVAL;
1261         }
1262
1263         /* Check that subport configuration is valid */
1264         s = port->subport + subport_id;
1265         if (s->tb_period == 0) {
1266                 RTE_LOG(ERR, SCHED,
1267                         "%s: Subport configuration invalid\n", __func__);
1268                 return -EINVAL;
1269         }
1270
1271         p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
1272
1273         /* Handle the case when pipe already has a valid configuration */
1274         if (p->tb_time) {
1275                 params = port->pipe_profiles + p->profile;
1276
1277                 double subport_tc_be_rate =
1278                         (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1279                         / (double) s->tc_period;
1280                 double pipe_tc_be_rate =
1281                         (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1282                         / (double) params->tc_period;
1283                 uint32_t tc_be_ov = s->tc_ov;
1284
1285                 /* Unplug pipe from its subport */
1286                 s->tc_ov_n -= params->tc_ov_weight;
1287                 s->tc_ov_rate -= pipe_tc_be_rate;
1288                 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1289
1290                 if (s->tc_ov != tc_be_ov) {
1291                         RTE_LOG(DEBUG, SCHED,
1292                                 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1293                                 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1294                 }
1295
1296                 /* Reset the pipe */
1297                 memset(p, 0, sizeof(struct rte_sched_pipe));
1298         }
1299
1300         if (deactivate)
1301                 return 0;
1302
1303         /* Apply the new pipe configuration */
1304         p->profile = profile;
1305         params = port->pipe_profiles + p->profile;
1306
1307         /* Token Bucket (TB) */
1308         p->tb_time = port->time;
1309         p->tb_credits = params->tb_size / 2;
1310
1311         /* Traffic Classes (TCs) */
1312         p->tc_time = port->time + params->tc_period;
1313
1314         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1315                 if (port->qsize[i])
1316                         p->tc_credits[i] = params->tc_credits_per_period[i];
1317
1318         {
1319                 /* Subport best effort tc oversubscription */
1320                 double subport_tc_be_rate =
1321                         (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1322                         / (double) s->tc_period;
1323                 double pipe_tc_be_rate =
1324                         (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1325                         / (double) params->tc_period;
1326                 uint32_t tc_be_ov = s->tc_ov;
1327
1328                 s->tc_ov_n += params->tc_ov_weight;
1329                 s->tc_ov_rate += pipe_tc_be_rate;
1330                 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1331
1332                 if (s->tc_ov != tc_be_ov) {
1333                         RTE_LOG(DEBUG, SCHED,
1334                                 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1335                                 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1336                 }
1337                 p->tc_ov_period_id = s->tc_ov_period_id;
1338                 p->tc_ov_credits = s->tc_ov_wm;
1339         }
1340
1341         return 0;
1342 }
1343
1344 int
1345 rte_sched_port_pipe_profile_add(struct rte_sched_port *port,
1346         struct rte_sched_pipe_params *params,
1347         uint32_t *pipe_profile_id)
1348 {
1349         struct rte_sched_pipe_profile *pp;
1350         uint32_t i;
1351         int status;
1352
1353         /* Port */
1354         if (port == NULL) {
1355                 RTE_LOG(ERR, SCHED,
1356                         "%s: Incorrect value for parameter port\n", __func__);
1357                 return -EINVAL;
1358         }
1359
1360         /* Pipe profiles not exceeds the max limit */
1361         if (port->n_pipe_profiles >= port->n_max_pipe_profiles) {
1362                 RTE_LOG(ERR, SCHED,
1363                         "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1364                 return -EINVAL;
1365         }
1366
1367         /* Pipe params */
1368         status = pipe_profile_check(params, port->rate, &port->qsize[0]);
1369         if (status != 0) {
1370                 RTE_LOG(ERR, SCHED,
1371                         "%s: Pipe profile check failed(%d)\n", __func__, status);
1372                 return -EINVAL;
1373         }
1374
1375         pp = &port->pipe_profiles[port->n_pipe_profiles];
1376         rte_sched_pipe_profile_convert(port, params, pp, port->rate);
1377
1378         /* Pipe profile not exists */
1379         for (i = 0; i < port->n_pipe_profiles; i++)
1380                 if (memcmp(port->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1381                         RTE_LOG(ERR, SCHED,
1382                                 "%s: Pipe profile doesn't exist\n", __func__);
1383                         return -EINVAL;
1384                 }
1385
1386         /* Pipe profile commit */
1387         *pipe_profile_id = port->n_pipe_profiles;
1388         port->n_pipe_profiles++;
1389
1390         if (port->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1391                 port->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1392
1393         rte_sched_port_log_pipe_profile(port, *pipe_profile_id);
1394
1395         return 0;
1396 }
1397
1398 static inline uint32_t
1399 rte_sched_port_qindex(struct rte_sched_port *port,
1400         uint32_t subport,
1401         uint32_t pipe,
1402         uint32_t traffic_class,
1403         uint32_t queue)
1404 {
1405         return ((subport & (port->n_subports_per_port - 1)) <<
1406                         (port->n_pipes_per_subport_log2 + 4)) |
1407                         ((pipe & (port->n_pipes_per_subport - 1)) << 4) |
1408                         ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1409                         (RTE_SCHED_QUEUES_PER_PIPE - 1));
1410 }
1411
1412 void
1413 rte_sched_port_pkt_write(struct rte_sched_port *port,
1414                          struct rte_mbuf *pkt,
1415                          uint32_t subport, uint32_t pipe,
1416                          uint32_t traffic_class,
1417                          uint32_t queue, enum rte_color color)
1418 {
1419         uint32_t queue_id =
1420                 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1421
1422         rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1423 }
1424
1425 void
1426 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1427                                   const struct rte_mbuf *pkt,
1428                                   uint32_t *subport, uint32_t *pipe,
1429                                   uint32_t *traffic_class, uint32_t *queue)
1430 {
1431         uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1432
1433         *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1434         *pipe = (queue_id >> 4) & (port->n_pipes_per_subport - 1);
1435         *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1436         *queue = rte_sched_port_tc_queue(port, queue_id);
1437 }
1438
1439 enum rte_color
1440 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1441 {
1442         return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1443 }
1444
1445 int
1446 rte_sched_subport_read_stats(struct rte_sched_port *port,
1447                              uint32_t subport_id,
1448                              struct rte_sched_subport_stats *stats,
1449                              uint32_t *tc_ov)
1450 {
1451         struct rte_sched_subport *s;
1452
1453         /* Check user parameters */
1454         if (port == NULL) {
1455                 RTE_LOG(ERR, SCHED,
1456                         "%s: Incorrect value for parameter port\n", __func__);
1457                 return -EINVAL;
1458         }
1459
1460         if (subport_id >= port->n_subports_per_port) {
1461                 RTE_LOG(ERR, SCHED,
1462                         "%s: Incorrect value for subport id\n", __func__);
1463                 return -EINVAL;
1464         }
1465
1466         if (stats == NULL) {
1467                 RTE_LOG(ERR, SCHED,
1468                         "%s: Incorrect value for parameter stats\n", __func__);
1469                 return -EINVAL;
1470         }
1471
1472         if (tc_ov == NULL) {
1473                 RTE_LOG(ERR, SCHED,
1474                         "%s: Incorrect value for tc_ov\n", __func__);
1475                 return -EINVAL;
1476         }
1477
1478         s = port->subport + subport_id;
1479
1480         /* Copy subport stats and clear */
1481         memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1482         memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1483
1484         /* Subport TC oversubscription status */
1485         *tc_ov = s->tc_ov;
1486
1487         return 0;
1488 }
1489
1490 int
1491 rte_sched_queue_read_stats(struct rte_sched_port *port,
1492         uint32_t queue_id,
1493         struct rte_sched_queue_stats *stats,
1494         uint16_t *qlen)
1495 {
1496         struct rte_sched_queue *q;
1497         struct rte_sched_queue_extra *qe;
1498
1499         /* Check user parameters */
1500         if (port == NULL) {
1501                 RTE_LOG(ERR, SCHED,
1502                         "%s: Incorrect value for parameter port\n", __func__);
1503                 return -EINVAL;
1504         }
1505
1506         if (queue_id >= rte_sched_port_queues_per_port(port)) {
1507                 RTE_LOG(ERR, SCHED,
1508                         "%s: Incorrect value for queue id\n", __func__);
1509                 return -EINVAL;
1510         }
1511
1512         if (stats == NULL) {
1513                 RTE_LOG(ERR, SCHED,
1514                         "%s: Incorrect value for parameter stats\n", __func__);
1515                 return -EINVAL;
1516         }
1517
1518         if (qlen == NULL) {
1519                 RTE_LOG(ERR, SCHED,
1520                         "%s: Incorrect value for parameter qlen\n", __func__);
1521                 return -EINVAL;
1522         }
1523         q = port->queue + queue_id;
1524         qe = port->queue_extra + queue_id;
1525
1526         /* Copy queue stats and clear */
1527         memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1528         memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1529
1530         /* Queue length */
1531         *qlen = q->qw - q->qr;
1532
1533         return 0;
1534 }
1535
1536 #ifdef RTE_SCHED_DEBUG
1537
1538 static inline int
1539 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1540 {
1541         struct rte_sched_queue *queue = port->queue + qindex;
1542
1543         return queue->qr == queue->qw;
1544 }
1545
1546 #endif /* RTE_SCHED_DEBUG */
1547
1548 #ifdef RTE_SCHED_COLLECT_STATS
1549
1550 static inline void
1551 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1552 {
1553         struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1554         uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1555         uint32_t pkt_len = pkt->pkt_len;
1556
1557         s->stats.n_pkts_tc[tc_index] += 1;
1558         s->stats.n_bytes_tc[tc_index] += pkt_len;
1559 }
1560
1561 #ifdef RTE_SCHED_RED
1562 static inline void
1563 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1564                                                 uint32_t qindex,
1565                                                 struct rte_mbuf *pkt, uint32_t red)
1566 #else
1567 static inline void
1568 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1569                                                 uint32_t qindex,
1570                                                 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1571 #endif
1572 {
1573         struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1574         uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1575         uint32_t pkt_len = pkt->pkt_len;
1576
1577         s->stats.n_pkts_tc_dropped[tc_index] += 1;
1578         s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1579 #ifdef RTE_SCHED_RED
1580         s->stats.n_pkts_red_dropped[tc_index] += red;
1581 #endif
1582 }
1583
1584 static inline void
1585 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1586 {
1587         struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1588         uint32_t pkt_len = pkt->pkt_len;
1589
1590         qe->stats.n_pkts += 1;
1591         qe->stats.n_bytes += pkt_len;
1592 }
1593
1594 #ifdef RTE_SCHED_RED
1595 static inline void
1596 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1597                                                 uint32_t qindex,
1598                                                 struct rte_mbuf *pkt, uint32_t red)
1599 #else
1600 static inline void
1601 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port,
1602                                                 uint32_t qindex,
1603                                                 struct rte_mbuf *pkt, __rte_unused uint32_t red)
1604 #endif
1605 {
1606         struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1607         uint32_t pkt_len = pkt->pkt_len;
1608
1609         qe->stats.n_pkts_dropped += 1;
1610         qe->stats.n_bytes_dropped += pkt_len;
1611 #ifdef RTE_SCHED_RED
1612         qe->stats.n_pkts_red_dropped += red;
1613 #endif
1614 }
1615
1616 #endif /* RTE_SCHED_COLLECT_STATS */
1617
1618 #ifdef RTE_SCHED_RED
1619
1620 static inline int
1621 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1622 {
1623         struct rte_sched_queue_extra *qe;
1624         struct rte_red_config *red_cfg;
1625         struct rte_red *red;
1626         uint32_t tc_index;
1627         enum rte_color color;
1628
1629         tc_index = rte_sched_port_pipe_tc(port, qindex);
1630         color = rte_sched_port_pkt_read_color(pkt);
1631         red_cfg = &port->red_config[tc_index][color];
1632
1633         if ((red_cfg->min_th | red_cfg->max_th) == 0)
1634                 return 0;
1635
1636         qe = port->queue_extra + qindex;
1637         red = &qe->red;
1638
1639         return rte_red_enqueue(red_cfg, red, qlen, port->time);
1640 }
1641
1642 static inline void
1643 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1644 {
1645         struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1646         struct rte_red *red = &qe->red;
1647
1648         rte_red_mark_queue_empty(red, port->time);
1649 }
1650
1651 #else
1652
1653 #define rte_sched_port_red_drop(port, pkt, qindex, qlen)             0
1654
1655 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1656
1657 #endif /* RTE_SCHED_RED */
1658
1659 #ifdef RTE_SCHED_DEBUG
1660
1661 static inline void
1662 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1663                        uint64_t bmp_slab)
1664 {
1665         uint64_t mask;
1666         uint32_t i, panic;
1667
1668         if (bmp_slab == 0)
1669                 rte_panic("Empty slab at position %u\n", bmp_pos);
1670
1671         panic = 0;
1672         for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1673                 if (mask & bmp_slab) {
1674                         if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1675                                 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1676                                 panic = 1;
1677                         }
1678                 }
1679         }
1680
1681         if (panic)
1682                 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1683                         bmp_slab, bmp_pos);
1684 }
1685
1686 #endif /* RTE_SCHED_DEBUG */
1687
1688 static inline uint32_t
1689 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port,
1690                                        struct rte_mbuf *pkt)
1691 {
1692         struct rte_sched_queue *q;
1693 #ifdef RTE_SCHED_COLLECT_STATS
1694         struct rte_sched_queue_extra *qe;
1695 #endif
1696         uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1697
1698         q = port->queue + qindex;
1699         rte_prefetch0(q);
1700 #ifdef RTE_SCHED_COLLECT_STATS
1701         qe = port->queue_extra + qindex;
1702         rte_prefetch0(qe);
1703 #endif
1704
1705         return qindex;
1706 }
1707
1708 static inline void
1709 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1710                                      uint32_t qindex, struct rte_mbuf **qbase)
1711 {
1712         struct rte_sched_queue *q;
1713         struct rte_mbuf **q_qw;
1714         uint16_t qsize;
1715
1716         q = port->queue + qindex;
1717         qsize = rte_sched_port_qsize(port, qindex);
1718         q_qw = qbase + (q->qw & (qsize - 1));
1719
1720         rte_prefetch0(q_qw);
1721         rte_bitmap_prefetch0(port->bmp, qindex);
1722 }
1723
1724 static inline int
1725 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex,
1726                            struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1727 {
1728         struct rte_sched_queue *q;
1729         uint16_t qsize;
1730         uint16_t qlen;
1731
1732         q = port->queue + qindex;
1733         qsize = rte_sched_port_qsize(port, qindex);
1734         qlen = q->qw - q->qr;
1735
1736         /* Drop the packet (and update drop stats) when queue is full */
1737         if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) ||
1738                      (qlen >= qsize))) {
1739                 rte_pktmbuf_free(pkt);
1740 #ifdef RTE_SCHED_COLLECT_STATS
1741                 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt,
1742                                                             qlen < qsize);
1743                 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt,
1744                                                           qlen < qsize);
1745 #endif
1746                 return 0;
1747         }
1748
1749         /* Enqueue packet */
1750         qbase[q->qw & (qsize - 1)] = pkt;
1751         q->qw++;
1752
1753         /* Activate queue in the port bitmap */
1754         rte_bitmap_set(port->bmp, qindex);
1755
1756         /* Statistics */
1757 #ifdef RTE_SCHED_COLLECT_STATS
1758         rte_sched_port_update_subport_stats(port, qindex, pkt);
1759         rte_sched_port_update_queue_stats(port, qindex, pkt);
1760 #endif
1761
1762         return 1;
1763 }
1764
1765
1766 /*
1767  * The enqueue function implements a 4-level pipeline with each stage
1768  * processing two different packets. The purpose of using a pipeline
1769  * is to hide the latency of prefetching the data structures. The
1770  * naming convention is presented in the diagram below:
1771  *
1772  *   p00  _______   p10  _______   p20  _______   p30  _______
1773  * ----->|       |----->|       |----->|       |----->|       |----->
1774  *       |   0   |      |   1   |      |   2   |      |   3   |
1775  * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1776  *   p01            p11            p21            p31
1777  *
1778  */
1779 int
1780 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1781                        uint32_t n_pkts)
1782 {
1783         struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1784                 *pkt30, *pkt31, *pkt_last;
1785         struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1786                 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1787         uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1788         uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1789         uint32_t result, i;
1790
1791         result = 0;
1792
1793         /*
1794          * Less then 6 input packets available, which is not enough to
1795          * feed the pipeline
1796          */
1797         if (unlikely(n_pkts < 6)) {
1798                 struct rte_mbuf **q_base[5];
1799                 uint32_t q[5];
1800
1801                 /* Prefetch the mbuf structure of each packet */
1802                 for (i = 0; i < n_pkts; i++)
1803                         rte_prefetch0(pkts[i]);
1804
1805                 /* Prefetch the queue structure for each queue */
1806                 for (i = 0; i < n_pkts; i++)
1807                         q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port,
1808                                                                       pkts[i]);
1809
1810                 /* Prefetch the write pointer location of each queue */
1811                 for (i = 0; i < n_pkts; i++) {
1812                         q_base[i] = rte_sched_port_qbase(port, q[i]);
1813                         rte_sched_port_enqueue_qwa_prefetch0(port, q[i],
1814                                                              q_base[i]);
1815                 }
1816
1817                 /* Write each packet to its queue */
1818                 for (i = 0; i < n_pkts; i++)
1819                         result += rte_sched_port_enqueue_qwa(port, q[i],
1820                                                              q_base[i], pkts[i]);
1821
1822                 return result;
1823         }
1824
1825         /* Feed the first 3 stages of the pipeline (6 packets needed) */
1826         pkt20 = pkts[0];
1827         pkt21 = pkts[1];
1828         rte_prefetch0(pkt20);
1829         rte_prefetch0(pkt21);
1830
1831         pkt10 = pkts[2];
1832         pkt11 = pkts[3];
1833         rte_prefetch0(pkt10);
1834         rte_prefetch0(pkt11);
1835
1836         q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1837         q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1838
1839         pkt00 = pkts[4];
1840         pkt01 = pkts[5];
1841         rte_prefetch0(pkt00);
1842         rte_prefetch0(pkt01);
1843
1844         q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1845         q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1846
1847         q20_base = rte_sched_port_qbase(port, q20);
1848         q21_base = rte_sched_port_qbase(port, q21);
1849         rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1850         rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1851
1852         /* Run the pipeline */
1853         for (i = 6; i < (n_pkts & (~1)); i += 2) {
1854                 /* Propagate stage inputs */
1855                 pkt30 = pkt20;
1856                 pkt31 = pkt21;
1857                 pkt20 = pkt10;
1858                 pkt21 = pkt11;
1859                 pkt10 = pkt00;
1860                 pkt11 = pkt01;
1861                 q30 = q20;
1862                 q31 = q21;
1863                 q20 = q10;
1864                 q21 = q11;
1865                 q30_base = q20_base;
1866                 q31_base = q21_base;
1867
1868                 /* Stage 0: Get packets in */
1869                 pkt00 = pkts[i];
1870                 pkt01 = pkts[i + 1];
1871                 rte_prefetch0(pkt00);
1872                 rte_prefetch0(pkt01);
1873
1874                 /* Stage 1: Prefetch queue structure storing queue pointers */
1875                 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1876                 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1877
1878                 /* Stage 2: Prefetch queue write location */
1879                 q20_base = rte_sched_port_qbase(port, q20);
1880                 q21_base = rte_sched_port_qbase(port, q21);
1881                 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1882                 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1883
1884                 /* Stage 3: Write packet to queue and activate queue */
1885                 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1886                 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1887                 result += r30 + r31;
1888         }
1889
1890         /*
1891          * Drain the pipeline (exactly 6 packets).
1892          * Handle the last packet in the case
1893          * of an odd number of input packets.
1894          */
1895         pkt_last = pkts[n_pkts - 1];
1896         rte_prefetch0(pkt_last);
1897
1898         q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1899         q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1900
1901         q10_base = rte_sched_port_qbase(port, q10);
1902         q11_base = rte_sched_port_qbase(port, q11);
1903         rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1904         rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1905
1906         r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1907         r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1908         result += r20 + r21;
1909
1910         q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1911
1912         q00_base = rte_sched_port_qbase(port, q00);
1913         q01_base = rte_sched_port_qbase(port, q01);
1914         rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1915         rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1916
1917         r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1918         r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1919         result += r10 + r11;
1920
1921         q_last_base = rte_sched_port_qbase(port, q_last);
1922         rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1923
1924         r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1925         r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1926         result += r00 + r01;
1927
1928         if (n_pkts & 1) {
1929                 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1930                 result += r_last;
1931         }
1932
1933         return result;
1934 }
1935
1936 #ifndef RTE_SCHED_SUBPORT_TC_OV
1937
1938 static inline void
1939 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1940 {
1941         struct rte_sched_grinder *grinder = port->grinder + pos;
1942         struct rte_sched_subport *subport = grinder->subport;
1943         struct rte_sched_pipe *pipe = grinder->pipe;
1944         struct rte_sched_pipe_profile *params = grinder->pipe_params;
1945         uint64_t n_periods;
1946         uint32_t i;
1947
1948         /* Subport TB */
1949         n_periods = (port->time - subport->tb_time) / subport->tb_period;
1950         subport->tb_credits += n_periods * subport->tb_credits_per_period;
1951         subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1952         subport->tb_time += n_periods * subport->tb_period;
1953
1954         /* Pipe TB */
1955         n_periods = (port->time - pipe->tb_time) / params->tb_period;
1956         pipe->tb_credits += n_periods * params->tb_credits_per_period;
1957         pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1958         pipe->tb_time += n_periods * params->tb_period;
1959
1960         /* Subport TCs */
1961         if (unlikely(port->time >= subport->tc_time)) {
1962                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1963                         subport->tc_credits[i] = subport->tc_credits_per_period[i];
1964
1965                 subport->tc_time = port->time + subport->tc_period;
1966         }
1967
1968         /* Pipe TCs */
1969         if (unlikely(port->time >= pipe->tc_time)) {
1970                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1971                         pipe->tc_credits[i] = params->tc_credits_per_period[i];
1972
1973                 pipe->tc_time = port->time + params->tc_period;
1974         }
1975 }
1976
1977 #else
1978
1979 static inline uint32_t
1980 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1981 {
1982         struct rte_sched_grinder *grinder = port->grinder + pos;
1983         struct rte_sched_subport *subport = grinder->subport;
1984         uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1985         uint32_t tc_consumption = 0, tc_ov_consumption_max;
1986         uint32_t tc_ov_wm = subport->tc_ov_wm;
1987         uint32_t i;
1988
1989         if (subport->tc_ov == 0)
1990                 return subport->tc_ov_wm_max;
1991
1992         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
1993                 tc_ov_consumption[i] =
1994                         subport->tc_credits_per_period[i] - subport->tc_credits[i];
1995                 tc_consumption += tc_ov_consumption[i];
1996         }
1997
1998         tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
1999                 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2000                 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2001
2002         tc_ov_consumption_max =
2003                 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2004                         tc_consumption;
2005
2006         if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2007                 (tc_ov_consumption_max - port->mtu)) {
2008                 tc_ov_wm  -= tc_ov_wm >> 7;
2009                 if (tc_ov_wm < subport->tc_ov_wm_min)
2010                         tc_ov_wm = subport->tc_ov_wm_min;
2011
2012                 return tc_ov_wm;
2013         }
2014
2015         tc_ov_wm += (tc_ov_wm >> 7) + 1;
2016         if (tc_ov_wm > subport->tc_ov_wm_max)
2017                 tc_ov_wm = subport->tc_ov_wm_max;
2018
2019         return tc_ov_wm;
2020 }
2021
2022 static inline void
2023 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
2024 {
2025         struct rte_sched_grinder *grinder = port->grinder + pos;
2026         struct rte_sched_subport *subport = grinder->subport;
2027         struct rte_sched_pipe *pipe = grinder->pipe;
2028         struct rte_sched_pipe_profile *params = grinder->pipe_params;
2029         uint64_t n_periods;
2030         uint32_t i;
2031
2032         /* Subport TB */
2033         n_periods = (port->time - subport->tb_time) / subport->tb_period;
2034         subport->tb_credits += n_periods * subport->tb_credits_per_period;
2035         subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
2036         subport->tb_time += n_periods * subport->tb_period;
2037
2038         /* Pipe TB */
2039         n_periods = (port->time - pipe->tb_time) / params->tb_period;
2040         pipe->tb_credits += n_periods * params->tb_credits_per_period;
2041         pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
2042         pipe->tb_time += n_periods * params->tb_period;
2043
2044         /* Subport TCs */
2045         if (unlikely(port->time >= subport->tc_time)) {
2046                 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
2047
2048                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2049                         subport->tc_credits[i] = subport->tc_credits_per_period[i];
2050
2051                 subport->tc_time = port->time + subport->tc_period;
2052                 subport->tc_ov_period_id++;
2053         }
2054
2055         /* Pipe TCs */
2056         if (unlikely(port->time >= pipe->tc_time)) {
2057                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2058                         pipe->tc_credits[i] = params->tc_credits_per_period[i];
2059                 pipe->tc_time = port->time + params->tc_period;
2060         }
2061
2062         /* Pipe TCs - Oversubscription */
2063         if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2064                 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2065
2066                 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2067         }
2068 }
2069
2070 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
2071
2072
2073 #ifndef RTE_SCHED_SUBPORT_TC_OV
2074
2075 static inline int
2076 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
2077 {
2078         struct rte_sched_grinder *grinder = port->grinder + pos;
2079         struct rte_sched_subport *subport = grinder->subport;
2080         struct rte_sched_pipe *pipe = grinder->pipe;
2081         struct rte_mbuf *pkt = grinder->pkt;
2082         uint32_t tc_index = grinder->tc_index;
2083         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2084         uint32_t subport_tb_credits = subport->tb_credits;
2085         uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2086         uint32_t pipe_tb_credits = pipe->tb_credits;
2087         uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2088         int enough_credits;
2089
2090         /* Check queue credits */
2091         enough_credits = (pkt_len <= subport_tb_credits) &&
2092                 (pkt_len <= subport_tc_credits) &&
2093                 (pkt_len <= pipe_tb_credits) &&
2094                 (pkt_len <= pipe_tc_credits);
2095
2096         if (!enough_credits)
2097                 return 0;
2098
2099         /* Update port credits */
2100         subport->tb_credits -= pkt_len;
2101         subport->tc_credits[tc_index] -= pkt_len;
2102         pipe->tb_credits -= pkt_len;
2103         pipe->tc_credits[tc_index] -= pkt_len;
2104
2105         return 1;
2106 }
2107
2108 #else
2109
2110 static inline int
2111 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
2112 {
2113         struct rte_sched_grinder *grinder = port->grinder + pos;
2114         struct rte_sched_subport *subport = grinder->subport;
2115         struct rte_sched_pipe *pipe = grinder->pipe;
2116         struct rte_mbuf *pkt = grinder->pkt;
2117         uint32_t tc_index = grinder->tc_index;
2118         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2119         uint32_t subport_tb_credits = subport->tb_credits;
2120         uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2121         uint32_t pipe_tb_credits = pipe->tb_credits;
2122         uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2123         uint32_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2124         uint32_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2125         uint32_t pipe_tc_ov_credits, i;
2126         int enough_credits;
2127
2128         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2129                 pipe_tc_ov_mask1[i] = UINT32_MAX;
2130
2131         pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2132         pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = UINT32_MAX;
2133         pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2134
2135         /* Check pipe and subport credits */
2136         enough_credits = (pkt_len <= subport_tb_credits) &&
2137                 (pkt_len <= subport_tc_credits) &&
2138                 (pkt_len <= pipe_tb_credits) &&
2139                 (pkt_len <= pipe_tc_credits) &&
2140                 (pkt_len <= pipe_tc_ov_credits);
2141
2142         if (!enough_credits)
2143                 return 0;
2144
2145         /* Update pipe and subport credits */
2146         subport->tb_credits -= pkt_len;
2147         subport->tc_credits[tc_index] -= pkt_len;
2148         pipe->tb_credits -= pkt_len;
2149         pipe->tc_credits[tc_index] -= pkt_len;
2150         pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2151
2152         return 1;
2153 }
2154
2155 #endif /* RTE_SCHED_SUBPORT_TC_OV */
2156
2157
2158 static inline int
2159 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
2160 {
2161         struct rte_sched_grinder *grinder = port->grinder + pos;
2162         struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2163         struct rte_mbuf *pkt = grinder->pkt;
2164         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2165         uint32_t be_tc_active;
2166
2167         if (!grinder_credits_check(port, pos))
2168                 return 0;
2169
2170         /* Advance port time */
2171         port->time += pkt_len;
2172
2173         /* Send packet */
2174         port->pkts_out[port->n_pkts_out++] = pkt;
2175         queue->qr++;
2176
2177         be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2178         grinder->wrr_tokens[grinder->qpos] +=
2179                 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2180
2181         if (queue->qr == queue->qw) {
2182                 uint32_t qindex = grinder->qindex[grinder->qpos];
2183
2184                 rte_bitmap_clear(port->bmp, qindex);
2185                 grinder->qmask &= ~(1 << grinder->qpos);
2186                 if (be_tc_active)
2187                         grinder->wrr_mask[grinder->qpos] = 0;
2188                 rte_sched_port_set_queue_empty_timestamp(port, qindex);
2189         }
2190
2191         /* Reset pipe loop detection */
2192         port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2193         grinder->productive = 1;
2194
2195         return 1;
2196 }
2197
2198 #ifdef SCHED_VECTOR_SSE4
2199
2200 static inline int
2201 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2202 {
2203         __m128i index = _mm_set1_epi32(base_pipe);
2204         __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
2205         __m128i res = _mm_cmpeq_epi32(pipes, index);
2206
2207         pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
2208         pipes = _mm_cmpeq_epi32(pipes, index);
2209         res = _mm_or_si128(res, pipes);
2210
2211         if (_mm_testz_si128(res, res))
2212                 return 0;
2213
2214         return 1;
2215 }
2216
2217 #elif defined(SCHED_VECTOR_NEON)
2218
2219 static inline int
2220 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2221 {
2222         uint32x4_t index, pipes;
2223         uint32_t *pos = (uint32_t *)port->grinder_base_bmp_pos;
2224
2225         index = vmovq_n_u32(base_pipe);
2226         pipes = vld1q_u32(pos);
2227         if (!vminvq_u32(veorq_u32(pipes, index)))
2228                 return 1;
2229
2230         pipes = vld1q_u32(pos + 4);
2231         if (!vminvq_u32(veorq_u32(pipes, index)))
2232                 return 1;
2233
2234         return 0;
2235 }
2236
2237 #else
2238
2239 static inline int
2240 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2241 {
2242         uint32_t i;
2243
2244         for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2245                 if (port->grinder_base_bmp_pos[i] == base_pipe)
2246                         return 1;
2247         }
2248
2249         return 0;
2250 }
2251
2252 #endif /* RTE_SCHED_OPTIMIZATIONS */
2253
2254 static inline void
2255 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2256 {
2257         struct rte_sched_grinder *grinder = port->grinder + pos;
2258         uint16_t w[4];
2259
2260         grinder->pcache_w = 0;
2261         grinder->pcache_r = 0;
2262
2263         w[0] = (uint16_t) bmp_slab;
2264         w[1] = (uint16_t) (bmp_slab >> 16);
2265         w[2] = (uint16_t) (bmp_slab >> 32);
2266         w[3] = (uint16_t) (bmp_slab >> 48);
2267
2268         grinder->pcache_qmask[grinder->pcache_w] = w[0];
2269         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2270         grinder->pcache_w += (w[0] != 0);
2271
2272         grinder->pcache_qmask[grinder->pcache_w] = w[1];
2273         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2274         grinder->pcache_w += (w[1] != 0);
2275
2276         grinder->pcache_qmask[grinder->pcache_w] = w[2];
2277         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2278         grinder->pcache_w += (w[2] != 0);
2279
2280         grinder->pcache_qmask[grinder->pcache_w] = w[3];
2281         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2282         grinder->pcache_w += (w[3] != 0);
2283 }
2284
2285 static inline void
2286 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
2287 {
2288         struct rte_sched_grinder *grinder = port->grinder + pos;
2289         uint8_t b, i;
2290
2291         grinder->tccache_w = 0;
2292         grinder->tccache_r = 0;
2293
2294         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2295                 b = (uint8_t) ((qmask >> i) & 0x1);
2296                 grinder->tccache_qmask[grinder->tccache_w] = b;
2297                 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2298                 grinder->tccache_w += (b != 0);
2299         }
2300
2301         b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2302         grinder->tccache_qmask[grinder->tccache_w] = b;
2303         grinder->tccache_qindex[grinder->tccache_w] = qindex +
2304                 RTE_SCHED_TRAFFIC_CLASS_BE;
2305         grinder->tccache_w += (b != 0);
2306 }
2307
2308 static inline int
2309 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
2310 {
2311         struct rte_sched_grinder *grinder = port->grinder + pos;
2312         struct rte_mbuf **qbase;
2313         uint32_t qindex;
2314         uint16_t qsize;
2315
2316         if (grinder->tccache_r == grinder->tccache_w)
2317                 return 0;
2318
2319         qindex = grinder->tccache_qindex[grinder->tccache_r];
2320         qbase = rte_sched_port_qbase(port, qindex);
2321         qsize = rte_sched_port_qsize(port, qindex);
2322
2323         grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2324         grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2325         grinder->qsize = qsize;
2326
2327         if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2328                 grinder->queue[0] = port->queue + qindex;
2329                 grinder->qbase[0] = qbase;
2330                 grinder->qindex[0] = qindex;
2331                 grinder->tccache_r++;
2332
2333                 return 1;
2334         }
2335
2336         grinder->queue[0] = port->queue + qindex;
2337         grinder->queue[1] = port->queue + qindex + 1;
2338         grinder->queue[2] = port->queue + qindex + 2;
2339         grinder->queue[3] = port->queue + qindex + 3;
2340
2341         grinder->qbase[0] = qbase;
2342         grinder->qbase[1] = qbase + qsize;
2343         grinder->qbase[2] = qbase + 2 * qsize;
2344         grinder->qbase[3] = qbase + 3 * qsize;
2345
2346         grinder->qindex[0] = qindex;
2347         grinder->qindex[1] = qindex + 1;
2348         grinder->qindex[2] = qindex + 2;
2349         grinder->qindex[3] = qindex + 3;
2350
2351         grinder->tccache_r++;
2352         return 1;
2353 }
2354
2355 static inline int
2356 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
2357 {
2358         struct rte_sched_grinder *grinder = port->grinder + pos;
2359         uint32_t pipe_qindex;
2360         uint16_t pipe_qmask;
2361
2362         if (grinder->pcache_r < grinder->pcache_w) {
2363                 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2364                 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2365                 grinder->pcache_r++;
2366         } else {
2367                 uint64_t bmp_slab = 0;
2368                 uint32_t bmp_pos = 0;
2369
2370                 /* Get another non-empty pipe group */
2371                 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
2372                         return 0;
2373
2374 #ifdef RTE_SCHED_DEBUG
2375                 debug_check_queue_slab(port, bmp_pos, bmp_slab);
2376 #endif
2377
2378                 /* Return if pipe group already in one of the other grinders */
2379                 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2380                 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
2381                         return 0;
2382
2383                 port->grinder_base_bmp_pos[pos] = bmp_pos;
2384
2385                 /* Install new pipe group into grinder's pipe cache */
2386                 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
2387
2388                 pipe_qmask = grinder->pcache_qmask[0];
2389                 pipe_qindex = grinder->pcache_qindex[0];
2390                 grinder->pcache_r = 1;
2391         }
2392
2393         /* Install new pipe in the grinder */
2394         grinder->pindex = pipe_qindex >> 4;
2395         grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
2396         grinder->pipe = port->pipe + grinder->pindex;
2397         grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2398         grinder->productive = 0;
2399
2400         grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
2401         grinder_next_tc(port, pos);
2402
2403         /* Check for pipe exhaustion */
2404         if (grinder->pindex == port->pipe_loop) {
2405                 port->pipe_exhaustion = 1;
2406                 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2407         }
2408
2409         return 1;
2410 }
2411
2412
2413 static inline void
2414 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
2415 {
2416         struct rte_sched_grinder *grinder = port->grinder + pos;
2417         struct rte_sched_pipe *pipe = grinder->pipe;
2418         struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2419         uint32_t qmask = grinder->qmask;
2420
2421         grinder->wrr_tokens[0] =
2422                 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2423         grinder->wrr_tokens[1] =
2424                 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2425         grinder->wrr_tokens[2] =
2426                 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2427         grinder->wrr_tokens[3] =
2428                 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2429
2430         grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2431         grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2432         grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2433         grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2434
2435         grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2436         grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2437         grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2438         grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2439 }
2440
2441 static inline void
2442 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
2443 {
2444         struct rte_sched_grinder *grinder = port->grinder + pos;
2445         struct rte_sched_pipe *pipe = grinder->pipe;
2446
2447         pipe->wrr_tokens[0] =
2448                         (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2449                                 RTE_SCHED_WRR_SHIFT;
2450         pipe->wrr_tokens[1] =
2451                         (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2452                                 RTE_SCHED_WRR_SHIFT;
2453         pipe->wrr_tokens[2] =
2454                         (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2455                                 RTE_SCHED_WRR_SHIFT;
2456         pipe->wrr_tokens[3] =
2457                         (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2458                                 RTE_SCHED_WRR_SHIFT;
2459 }
2460
2461 static inline void
2462 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
2463 {
2464         struct rte_sched_grinder *grinder = port->grinder + pos;
2465         uint16_t wrr_tokens_min;
2466
2467         grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2468         grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2469         grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2470         grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2471
2472         grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2473         wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2474
2475         grinder->wrr_tokens[0] -= wrr_tokens_min;
2476         grinder->wrr_tokens[1] -= wrr_tokens_min;
2477         grinder->wrr_tokens[2] -= wrr_tokens_min;
2478         grinder->wrr_tokens[3] -= wrr_tokens_min;
2479 }
2480
2481
2482 #define grinder_evict(port, pos)
2483
2484 static inline void
2485 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
2486 {
2487         struct rte_sched_grinder *grinder = port->grinder + pos;
2488
2489         rte_prefetch0(grinder->pipe);
2490         rte_prefetch0(grinder->queue[0]);
2491 }
2492
2493 static inline void
2494 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
2495 {
2496         struct rte_sched_grinder *grinder = port->grinder + pos;
2497         uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2498
2499         qsize = grinder->qsize;
2500         grinder->qpos = 0;
2501
2502         if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2503                 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2504
2505                 rte_prefetch0(grinder->qbase[0] + qr[0]);
2506                 return;
2507         }
2508
2509         qr[0] = grinder->queue[0]->qr & (qsize - 1);
2510         qr[1] = grinder->queue[1]->qr & (qsize - 1);
2511         qr[2] = grinder->queue[2]->qr & (qsize - 1);
2512         qr[3] = grinder->queue[3]->qr & (qsize - 1);
2513
2514         rte_prefetch0(grinder->qbase[0] + qr[0]);
2515         rte_prefetch0(grinder->qbase[1] + qr[1]);
2516
2517         grinder_wrr_load(port, pos);
2518         grinder_wrr(port, pos);
2519
2520         rte_prefetch0(grinder->qbase[2] + qr[2]);
2521         rte_prefetch0(grinder->qbase[3] + qr[3]);
2522 }
2523
2524 static inline void
2525 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2526 {
2527         struct rte_sched_grinder *grinder = port->grinder + pos;
2528         uint32_t qpos = grinder->qpos;
2529         struct rte_mbuf **qbase = grinder->qbase[qpos];
2530         uint16_t qsize = grinder->qsize;
2531         uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2532
2533         grinder->pkt = qbase[qr];
2534         rte_prefetch0(grinder->pkt);
2535
2536         if (unlikely((qr & 0x7) == 7)) {
2537                 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2538
2539                 rte_prefetch0(qbase + qr_next);
2540         }
2541 }
2542
2543 static inline uint32_t
2544 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2545 {
2546         struct rte_sched_grinder *grinder = port->grinder + pos;
2547
2548         switch (grinder->state) {
2549         case e_GRINDER_PREFETCH_PIPE:
2550         {
2551                 if (grinder_next_pipe(port, pos)) {
2552                         grinder_prefetch_pipe(port, pos);
2553                         port->busy_grinders++;
2554
2555                         grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2556                         return 0;
2557                 }
2558
2559                 return 0;
2560         }
2561
2562         case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2563         {
2564                 struct rte_sched_pipe *pipe = grinder->pipe;
2565
2566                 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2567                 grinder_prefetch_tc_queue_arrays(port, pos);
2568                 grinder_credits_update(port, pos);
2569
2570                 grinder->state = e_GRINDER_PREFETCH_MBUF;
2571                 return 0;
2572         }
2573
2574         case e_GRINDER_PREFETCH_MBUF:
2575         {
2576                 grinder_prefetch_mbuf(port, pos);
2577
2578                 grinder->state = e_GRINDER_READ_MBUF;
2579                 return 0;
2580         }
2581
2582         case e_GRINDER_READ_MBUF:
2583         {
2584                 uint32_t wrr_active, result = 0;
2585
2586                 result = grinder_schedule(port, pos);
2587
2588                 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2589
2590                 /* Look for next packet within the same TC */
2591                 if (result && grinder->qmask) {
2592                         if (wrr_active)
2593                                 grinder_wrr(port, pos);
2594
2595                         grinder_prefetch_mbuf(port, pos);
2596
2597                         return 1;
2598                 }
2599
2600                 if (wrr_active)
2601                         grinder_wrr_store(port, pos);
2602
2603                 /* Look for another active TC within same pipe */
2604                 if (grinder_next_tc(port, pos)) {
2605                         grinder_prefetch_tc_queue_arrays(port, pos);
2606
2607                         grinder->state = e_GRINDER_PREFETCH_MBUF;
2608                         return result;
2609                 }
2610
2611                 if (grinder->productive == 0 &&
2612                     port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2613                         port->pipe_loop = grinder->pindex;
2614
2615                 grinder_evict(port, pos);
2616
2617                 /* Look for another active pipe */
2618                 if (grinder_next_pipe(port, pos)) {
2619                         grinder_prefetch_pipe(port, pos);
2620
2621                         grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2622                         return result;
2623                 }
2624
2625                 /* No active pipe found */
2626                 port->busy_grinders--;
2627
2628                 grinder->state = e_GRINDER_PREFETCH_PIPE;
2629                 return result;
2630         }
2631
2632         default:
2633                 rte_panic("Algorithmic error (invalid state)\n");
2634                 return 0;
2635         }
2636 }
2637
2638 static inline void
2639 rte_sched_port_time_resync(struct rte_sched_port *port)
2640 {
2641         uint64_t cycles = rte_get_tsc_cycles();
2642         uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2643         uint64_t bytes_diff;
2644
2645         /* Compute elapsed time in bytes */
2646         bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2647                                            port->inv_cycles_per_byte);
2648
2649         /* Advance port time */
2650         port->time_cpu_cycles = cycles;
2651         port->time_cpu_bytes += bytes_diff;
2652         if (port->time < port->time_cpu_bytes)
2653                 port->time = port->time_cpu_bytes;
2654
2655         /* Reset pipe loop detection */
2656         port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2657 }
2658
2659 static inline int
2660 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2661 {
2662         int exceptions;
2663
2664         /* Check if any exception flag is set */
2665         exceptions = (second_pass && port->busy_grinders == 0) ||
2666                 (port->pipe_exhaustion == 1);
2667
2668         /* Clear exception flags */
2669         port->pipe_exhaustion = 0;
2670
2671         return exceptions;
2672 }
2673
2674 int
2675 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2676 {
2677         uint32_t i, count;
2678
2679         port->pkts_out = pkts;
2680         port->n_pkts_out = 0;
2681
2682         rte_sched_port_time_resync(port);
2683
2684         /* Take each queue in the grinder one step further */
2685         for (i = 0, count = 0; ; i++)  {
2686                 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2687                 if ((count == n_pkts) ||
2688                     rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {
2689                         break;
2690                 }
2691         }
2692
2693         return count;
2694 }