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