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