sched: make RED optional at runtime
[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 min/max are both zero, then RED is disabled */
640                         if ((params->red_params[i][j].min_th |
641                              params->red_params[i][j].max_th) == 0) {
642                                 continue;
643                         }
644
645                         if (rte_red_config_init(&port->red_config[i][j],
646                                 params->red_params[i][j].wq_log2,
647                                 params->red_params[i][j].min_th,
648                                 params->red_params[i][j].max_th,
649                                 params->red_params[i][j].maxp_inv) != 0) {
650                                 return NULL;
651                         }
652                 }
653         }
654 #endif
655
656         /* Timing */
657         port->time_cpu_cycles = rte_get_tsc_cycles();
658         port->time_cpu_bytes = 0;
659         port->time = 0;
660         port->cycles_per_byte = ((double) rte_get_tsc_hz()) / ((double) params->rate);
661
662         /* Scheduling loop detection */
663         port->pipe_loop = RTE_SCHED_PIPE_INVALID;
664         port->pipe_exhaustion = 0;
665
666         /* Grinders */
667         port->busy_grinders = 0;
668         port->pkts_out = NULL;
669         port->n_pkts_out = 0;
670
671         /* Queue base calculation */
672         rte_sched_port_config_qsize(port);
673
674         /* Large data structures */
675         port->subport = (struct rte_sched_subport *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_SUBPORT));
676         port->pipe = (struct rte_sched_pipe *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_PIPE));
677         port->queue = (struct rte_sched_queue *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_QUEUE));
678         port->queue_extra = (struct rte_sched_queue_extra *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA));
679         port->pipe_profiles = (struct rte_sched_pipe_profile *) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES));
680         port->bmp_array =  port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY);
681         port->queue_array = (struct rte_mbuf **) (port->memory + rte_sched_port_get_array_base(params, e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY));
682
683         /* Pipe profile table */
684         rte_sched_port_config_pipe_profile_table(port, params);
685
686         /* Bitmap */
687         n_queues_per_port = rte_sched_port_queues_per_port(port);
688         bmp_mem_size = rte_bitmap_get_memory_footprint(n_queues_per_port);
689         port->bmp = rte_bitmap_init(n_queues_per_port, port->bmp_array, bmp_mem_size);
690         if (port->bmp == NULL) {
691                 RTE_LOG(INFO, SCHED, "Bitmap init error\n");
692                 return NULL;
693         }
694         for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i ++) {
695                 port->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
696         }
697
698         return port;
699 }
700
701 void
702 rte_sched_port_free(struct rte_sched_port *port)
703 {
704         /* Check user parameters */
705         if (port == NULL){
706                 return;
707         }
708
709         rte_bitmap_free(port->bmp);
710         rte_free(port);
711 }
712
713 static void
714 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
715 {
716         struct rte_sched_subport *s = port->subport + i;
717
718         RTE_LOG(INFO, SCHED, "Low level config for subport %u:\n"
719                 "\tToken bucket: period = %u, credits per period = %u, size = %u\n"
720                 "\tTraffic classes: period = %u, credits per period = [%u, %u, %u, %u]\n"
721                 "\tTraffic class 3 oversubscription: wm min = %u, wm max = %u\n",
722                 i,
723
724                 /* Token bucket */
725                 s->tb_period,
726                 s->tb_credits_per_period,
727                 s->tb_size,
728
729                 /* Traffic classes */
730                 s->tc_period,
731                 s->tc_credits_per_period[0],
732                 s->tc_credits_per_period[1],
733                 s->tc_credits_per_period[2],
734                 s->tc_credits_per_period[3],
735
736                 /* Traffic class 3 oversubscription */
737                 s->tc_ov_wm_min,
738                 s->tc_ov_wm_max);
739 }
740
741 int
742 rte_sched_subport_config(struct rte_sched_port *port,
743         uint32_t subport_id,
744         struct rte_sched_subport_params *params)
745 {
746         struct rte_sched_subport *s;
747         uint32_t i;
748
749         /* Check user parameters */
750         if ((port == NULL) ||
751             (subport_id >= port->n_subports_per_port) ||
752                 (params == NULL)) {
753                 return -1;
754         }
755
756         if ((params->tb_rate == 0) || (params->tb_rate > port->rate)) {
757                 return -2;
758         }
759
760         if (params->tb_size == 0) {
761                 return -3;
762         }
763
764         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
765                 if ((params->tc_rate[i] == 0) || (params->tc_rate[i] > params->tb_rate)) {
766                         return -4;
767                 }
768         }
769
770         if (params->tc_period == 0) {
771                 return -5;
772         }
773
774         s = port->subport + subport_id;
775
776         /* Token Bucket (TB) */
777         if (params->tb_rate == port->rate) {
778                 s->tb_credits_per_period = 1;
779                 s->tb_period = 1;
780         } else {
781                 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
782                 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
783
784                 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
785         }
786         s->tb_size = params->tb_size;
787         s->tb_time = port->time;
788         s->tb_credits = s->tb_size / 2;
789
790         /* Traffic Classes (TCs) */
791         s->tc_period = (uint32_t) rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
792         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
793                 s->tc_credits_per_period[i] = (uint32_t) rte_sched_time_ms_to_bytes(params->tc_period, params->tc_rate[i]);
794         }
795         s->tc_time = port->time + s->tc_period;
796         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
797                 s->tc_credits[i] = s->tc_credits_per_period[i];
798         }
799
800 #ifdef RTE_SCHED_SUBPORT_TC_OV
801         /* TC oversubscription */
802         s->tc_ov_wm_min = port->mtu;
803         s->tc_ov_wm_max = (uint32_t) rte_sched_time_ms_to_bytes(params->tc_period, port->pipe_tc3_rate_max);
804         s->tc_ov_wm = s->tc_ov_wm_max;
805         s->tc_ov_period_id = 0;
806         s->tc_ov = 0;
807         s->tc_ov_n = 0;
808         s->tc_ov_rate = 0;
809 #endif
810
811         rte_sched_port_log_subport_config(port, subport_id);
812
813         return 0;
814 }
815
816 int
817 rte_sched_pipe_config(struct rte_sched_port *port,
818         uint32_t subport_id,
819         uint32_t pipe_id,
820         int32_t pipe_profile)
821 {
822         struct rte_sched_subport *s;
823         struct rte_sched_pipe *p;
824         struct rte_sched_pipe_profile *params;
825         uint32_t deactivate, profile, i;
826
827         /* Check user parameters */
828         profile = (uint32_t) pipe_profile;
829         deactivate = (pipe_profile < 0);
830         if ((port == NULL) ||
831             (subport_id >= port->n_subports_per_port) ||
832                 (pipe_id >= port->n_pipes_per_subport) ||
833                 ((!deactivate) && (profile >= port->n_pipe_profiles))) {
834                 return -1;
835         }
836
837         /* Check that subport configuration is valid */
838         s = port->subport + subport_id;
839         if (s->tb_period == 0) {
840                 return -2;
841         }
842
843         p = port->pipe + (subport_id * port->n_pipes_per_subport + pipe_id);
844
845         /* Handle the case when pipe already has a valid configuration */
846         if (p->tb_time) {
847                 params = port->pipe_profiles + p->profile;
848
849 #ifdef RTE_SCHED_SUBPORT_TC_OV
850                 double subport_tc3_rate = ((double) s->tc_credits_per_period[3]) / ((double) s->tc_period);
851                 double pipe_tc3_rate = ((double) params->tc_credits_per_period[3]) / ((double) params->tc_period);
852                 uint32_t tc3_ov = s->tc_ov;
853
854                 /* Unplug pipe from its subport */
855                 s->tc_ov_n -= params->tc_ov_weight;
856                 s->tc_ov_rate -= pipe_tc3_rate;
857                 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
858
859                 if (s->tc_ov != tc3_ov) {
860                         RTE_LOG(INFO, SCHED, "Subport %u TC3 oversubscription is OFF (%.4lf >= %.4lf)\n",
861                                 subport_id, subport_tc3_rate, s->tc_ov_rate);
862                 }
863 #endif
864
865                 /* Reset the pipe */
866                 memset(p, 0, sizeof(struct rte_sched_pipe));
867         }
868
869         if (deactivate) {
870                 return 0;
871         }
872
873         /* Apply the new pipe configuration */
874         p->profile = profile;
875         params = port->pipe_profiles + p->profile;
876
877         /* Token Bucket (TB) */
878         p->tb_time = port->time;
879         p->tb_credits = params->tb_size / 2;
880
881         /* Traffic Classes (TCs) */
882         p->tc_time = port->time + params->tc_period;
883         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i ++) {
884                 p->tc_credits[i] = params->tc_credits_per_period[i];
885         }
886
887 #ifdef RTE_SCHED_SUBPORT_TC_OV
888         {
889                 /* Subport TC3 oversubscription */
890                 double subport_tc3_rate = ((double) s->tc_credits_per_period[3]) / ((double) s->tc_period);
891                 double pipe_tc3_rate = ((double) params->tc_credits_per_period[3]) / ((double) params->tc_period);
892                 uint32_t tc3_ov = s->tc_ov;
893
894                 s->tc_ov_n += params->tc_ov_weight;
895                 s->tc_ov_rate += pipe_tc3_rate;
896                 s->tc_ov = s->tc_ov_rate > subport_tc3_rate;
897
898                 if (s->tc_ov != tc3_ov) {
899                         RTE_LOG(INFO, SCHED, "Subport %u TC3 oversubscription is ON (%.4lf < %.4lf)\n",
900                                 subport_id, subport_tc3_rate, s->tc_ov_rate);
901                 }
902                 p->tc_ov_period_id = s->tc_ov_period_id;
903                 p->tc_ov_credits = s->tc_ov_wm;
904         }
905 #endif
906
907         return 0;
908 }
909
910 int
911 rte_sched_subport_read_stats(struct rte_sched_port *port,
912         uint32_t subport_id,
913         struct rte_sched_subport_stats *stats,
914         uint32_t *tc_ov)
915 {
916         struct rte_sched_subport *s;
917
918         /* Check user parameters */
919         if ((port == NULL) ||
920             (subport_id >= port->n_subports_per_port) ||
921                 (stats == NULL) ||
922                 (tc_ov == NULL)) {
923                 return -1;
924         }
925         s = port->subport + subport_id;
926
927         /* Copy subport stats and clear */
928         memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
929         memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
930
931         /* Subport TC ovesubscription status */
932         *tc_ov = s->tc_ov;
933
934         return 0;
935 }
936
937 int
938 rte_sched_queue_read_stats(struct rte_sched_port *port,
939         uint32_t queue_id,
940         struct rte_sched_queue_stats *stats,
941         uint16_t *qlen)
942 {
943         struct rte_sched_queue *q;
944         struct rte_sched_queue_extra *qe;
945
946         /* Check user parameters */
947         if ((port == NULL) ||
948             (queue_id >= rte_sched_port_queues_per_port(port)) ||
949                 (stats == NULL) ||
950                 (qlen == NULL)) {
951                 return -1;
952         }
953         q = port->queue + queue_id;
954         qe = port->queue_extra + queue_id;
955
956         /* Copy queue stats and clear */
957         memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
958         memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
959
960         /* Queue length */
961         *qlen = q->qw - q->qr;
962
963         return 0;
964 }
965
966 static inline uint32_t
967 rte_sched_port_qindex(struct rte_sched_port *port, uint32_t subport, uint32_t pipe, uint32_t traffic_class, uint32_t queue)
968 {
969         uint32_t result;
970
971         result = subport * port->n_pipes_per_subport + pipe;
972         result = result * RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE + traffic_class;
973         result = result * RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS + queue;
974
975         return result;
976 }
977
978 static inline struct rte_mbuf **
979 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
980 {
981         uint32_t pindex = qindex >> 4;
982         uint32_t qpos = qindex & 0xF;
983
984         return (port->queue_array + pindex * port->qsize_sum + port->qsize_add[qpos]);
985 }
986
987 static inline uint16_t
988 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
989 {
990         uint32_t tc = (qindex >> 2) & 0x3;
991
992         return port->qsize[tc];
993 }
994
995 #if RTE_SCHED_DEBUG
996
997 static inline int
998 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
999 {
1000         struct rte_sched_queue *queue = port->queue + qindex;
1001
1002         return (queue->qr == queue->qw);
1003 }
1004
1005 static inline int
1006 rte_sched_port_queue_is_full(struct rte_sched_port *port, uint32_t qindex)
1007 {
1008         struct rte_sched_queue *queue = port->queue + qindex;
1009         uint16_t qsize = rte_sched_port_qsize(port, qindex);
1010         uint16_t qlen = queue->qw - queue->qr;
1011
1012         return (qlen >= qsize);
1013 }
1014
1015 #endif /* RTE_SCHED_DEBUG */
1016
1017 #ifdef RTE_SCHED_COLLECT_STATS
1018
1019 static inline void
1020 rte_sched_port_update_subport_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1021 {
1022         struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1023         uint32_t tc_index = (qindex >> 2) & 0x3;
1024         uint32_t pkt_len = pkt->pkt_len;
1025
1026         s->stats.n_pkts_tc[tc_index] += 1;
1027         s->stats.n_bytes_tc[tc_index] += pkt_len;
1028 }
1029
1030 static inline void
1031 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1032 {
1033         struct rte_sched_subport *s = port->subport + (qindex / rte_sched_port_queues_per_subport(port));
1034         uint32_t tc_index = (qindex >> 2) & 0x3;
1035         uint32_t pkt_len = pkt->pkt_len;
1036
1037         s->stats.n_pkts_tc_dropped[tc_index] += 1;
1038         s->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1039 }
1040
1041 static inline void
1042 rte_sched_port_update_queue_stats(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1043 {
1044         struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1045         uint32_t pkt_len = pkt->pkt_len;
1046
1047         qe->stats.n_pkts += 1;
1048         qe->stats.n_bytes += pkt_len;
1049 }
1050
1051 static inline void
1052 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf *pkt)
1053 {
1054         struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1055         uint32_t pkt_len = pkt->pkt_len;
1056
1057         qe->stats.n_pkts_dropped += 1;
1058         qe->stats.n_bytes_dropped += pkt_len;
1059 }
1060
1061 #endif /* RTE_SCHED_COLLECT_STATS */
1062
1063 #ifdef RTE_SCHED_RED
1064
1065 static inline int
1066 rte_sched_port_red_drop(struct rte_sched_port *port, struct rte_mbuf *pkt, uint32_t qindex, uint16_t qlen)
1067 {
1068         struct rte_sched_queue_extra *qe;
1069         struct rte_red_config *red_cfg;
1070     struct rte_red *red;
1071         uint32_t tc_index;
1072         enum rte_meter_color color;
1073
1074         tc_index = (qindex >> 2) & 0x3;
1075         color = rte_sched_port_pkt_read_color(pkt);
1076         red_cfg = &port->red_config[tc_index][color];
1077
1078         if ((red_cfg->min_th | red_cfg->max_th) == 0)
1079                 return 0;
1080
1081         qe = port->queue_extra + qindex;
1082         red = &qe->red;
1083
1084         return rte_red_enqueue(red_cfg, red, qlen, port->time);
1085 }
1086
1087 static inline void
1088 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1089 {
1090         struct rte_sched_queue_extra *qe;
1091     struct rte_red *red;
1092
1093         qe = port->queue_extra + qindex;
1094         red = &qe->red;
1095
1096         rte_red_mark_queue_empty(red, port->time);
1097 }
1098
1099 #else
1100
1101 #define rte_sched_port_red_drop(port, pkt, qindex, qlen)             0
1102
1103 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1104
1105 #endif /* RTE_SCHED_RED */
1106
1107 #if RTE_SCHED_DEBUG
1108
1109 static inline int
1110 debug_pipe_is_empty(struct rte_sched_port *port, uint32_t pindex)
1111 {
1112         uint32_t qindex, i;
1113
1114         qindex = pindex << 4;
1115
1116         for (i = 0; i < 16; i ++){
1117                 uint32_t queue_empty = rte_sched_port_queue_is_empty(port, qindex + i);
1118                 uint32_t bmp_bit_clear = (rte_bitmap_get(port->bmp, qindex + i) == 0);
1119
1120                 if (queue_empty != bmp_bit_clear){
1121                         rte_panic("Queue status mismatch for queue %u of pipe %u\n", i, pindex);
1122                 }
1123
1124                 if (!queue_empty){
1125                         return 0;
1126                 }
1127         }
1128
1129         return 1;
1130 }
1131
1132 static inline void
1133 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos, uint64_t bmp_slab)
1134 {
1135         uint64_t mask;
1136         uint32_t i, panic;
1137
1138         if (bmp_slab == 0){
1139                 rte_panic("Empty slab at position %u\n", bmp_pos);
1140         }
1141
1142         panic = 0;
1143         for (i = 0, mask = 1; i < 64; i ++, mask <<= 1) {
1144                 if (mask & bmp_slab){
1145                         if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1146                                 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1147                                 panic = 1;
1148                         }
1149                 }
1150         }
1151
1152         if (panic){
1153                 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1154                         bmp_slab, bmp_pos);
1155         }
1156 }
1157
1158 #endif /* RTE_SCHED_DEBUG */
1159
1160 static inline uint32_t
1161 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_port *port, struct rte_mbuf *pkt)
1162 {
1163         struct rte_sched_queue *q;
1164 #ifdef RTE_SCHED_COLLECT_STATS
1165         struct rte_sched_queue_extra *qe;
1166 #endif
1167         uint32_t subport, pipe, traffic_class, queue, qindex;
1168
1169         rte_sched_port_pkt_read_tree_path(pkt, &subport, &pipe, &traffic_class, &queue);
1170
1171         qindex = rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1172         q = port->queue + qindex;
1173         rte_prefetch0(q);
1174 #ifdef RTE_SCHED_COLLECT_STATS
1175         qe = port->queue_extra + qindex;
1176         rte_prefetch0(qe);
1177 #endif
1178
1179         return qindex;
1180 }
1181
1182 static inline void
1183 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf **qbase)
1184 {
1185         struct rte_sched_queue *q;
1186         struct rte_mbuf **q_qw;
1187         uint16_t qsize;
1188
1189         q = port->queue + qindex;
1190         qsize = rte_sched_port_qsize(port, qindex);
1191         q_qw = qbase + (q->qw & (qsize - 1));
1192
1193         rte_prefetch0(q_qw);
1194         rte_bitmap_prefetch0(port->bmp, qindex);
1195 }
1196
1197 static inline int
1198 rte_sched_port_enqueue_qwa(struct rte_sched_port *port, uint32_t qindex, struct rte_mbuf **qbase, struct rte_mbuf *pkt)
1199 {
1200         struct rte_sched_queue *q;
1201         uint16_t qsize;
1202         uint16_t qlen;
1203
1204         q = port->queue + qindex;
1205         qsize = rte_sched_port_qsize(port, qindex);
1206         qlen = q->qw - q->qr;
1207
1208         /* Drop the packet (and update drop stats) when queue is full */
1209         if (unlikely(rte_sched_port_red_drop(port, pkt, qindex, qlen) || (qlen >= qsize))) {
1210                 rte_pktmbuf_free(pkt);
1211 #ifdef RTE_SCHED_COLLECT_STATS
1212                 rte_sched_port_update_subport_stats_on_drop(port, qindex, pkt);
1213                 rte_sched_port_update_queue_stats_on_drop(port, qindex, pkt);
1214 #endif
1215                 return 0;
1216         }
1217
1218         /* Enqueue packet */
1219         qbase[q->qw & (qsize - 1)] = pkt;
1220         q->qw ++;
1221
1222         /* Activate queue in the port bitmap */
1223         rte_bitmap_set(port->bmp, qindex);
1224
1225         /* Statistics */
1226 #ifdef RTE_SCHED_COLLECT_STATS
1227         rte_sched_port_update_subport_stats(port, qindex, pkt);
1228         rte_sched_port_update_queue_stats(port, qindex, pkt);
1229 #endif
1230
1231         return 1;
1232 }
1233
1234 #if RTE_SCHED_ENQUEUE == 0
1235
1236 int
1237 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
1238 {
1239         uint32_t result, i;
1240
1241         result = 0;
1242
1243         for (i = 0; i < n_pkts; i ++) {
1244                 struct rte_mbuf *pkt;
1245                 struct rte_mbuf **q_base;
1246                 uint32_t subport, pipe, traffic_class, queue, qindex;
1247
1248                 pkt = pkts[i];
1249
1250                 rte_sched_port_pkt_read_tree_path(pkt, &subport, &pipe, &traffic_class, &queue);
1251
1252                 qindex = rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1253
1254                 q_base = rte_sched_port_qbase(port, qindex);
1255
1256                 result += rte_sched_port_enqueue_qwa(port, qindex, q_base, pkt);
1257         }
1258
1259         return result;
1260 }
1261
1262 #else
1263
1264 /* The enqueue function implements a 4-level pipeline with each stage processing
1265  * two different packets. The purpose of using a pipeline is to hide the latency
1266  * of prefetching the data structures. The naming convention is presented in the
1267  * diagram below:
1268  *
1269  *   p00  _______   p10  _______   p20  _______   p30  _______
1270  * ----->|       |----->|       |----->|       |----->|       |----->
1271  *       |   0   |      |   1   |      |   2   |      |   3   |
1272  * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1273  *   p01            p11            p21            p31
1274  *
1275  ***/
1276 int
1277 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
1278 {
1279         struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21, *pkt30, *pkt31, *pkt_last;
1280         struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base, **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1281         uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1282         uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1283         uint32_t result, i;
1284
1285         result = 0;
1286
1287         /* Less then 6 input packets available, which is not enough to feed the pipeline */
1288         if (unlikely(n_pkts < 6)) {
1289                 struct rte_mbuf **q_base[5];
1290                 uint32_t q[5];
1291
1292                 /* Prefetch the mbuf structure of each packet */
1293                 for (i = 0; i < n_pkts; i ++) {
1294                         rte_prefetch0(pkts[i]);
1295                 }
1296
1297                 /* Prefetch the queue structure for each queue */
1298                 for (i = 0; i < n_pkts; i ++) {
1299                         q[i] = rte_sched_port_enqueue_qptrs_prefetch0(port, pkts[i]);
1300                 }
1301
1302                 /* Prefetch the write pointer location of each queue */
1303                 for (i = 0; i < n_pkts; i ++) {
1304                         q_base[i] = rte_sched_port_qbase(port, q[i]);
1305                         rte_sched_port_enqueue_qwa_prefetch0(port, q[i], q_base[i]);
1306                 }
1307
1308                 /* Write each packet to its queue */
1309                 for (i = 0; i < n_pkts; i ++) {
1310                         result += rte_sched_port_enqueue_qwa(port, q[i], q_base[i], pkts[i]);
1311                 }
1312
1313                 return result;
1314         }
1315
1316         /* Feed the first 3 stages of the pipeline (6 packets needed) */
1317         pkt20 = pkts[0];
1318         pkt21 = pkts[1];
1319         rte_prefetch0(pkt20);
1320         rte_prefetch0(pkt21);
1321
1322         pkt10 = pkts[2];
1323         pkt11 = pkts[3];
1324         rte_prefetch0(pkt10);
1325         rte_prefetch0(pkt11);
1326
1327         q20 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt20);
1328         q21 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt21);
1329
1330         pkt00 = pkts[4];
1331         pkt01 = pkts[5];
1332         rte_prefetch0(pkt00);
1333         rte_prefetch0(pkt01);
1334
1335         q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1336         q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1337
1338         q20_base = rte_sched_port_qbase(port, q20);
1339         q21_base = rte_sched_port_qbase(port, q21);
1340         rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1341         rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1342
1343         /* Run the pipeline */
1344         for (i = 6; i < (n_pkts & (~1)); i += 2) {
1345                 /* Propagate stage inputs */
1346                 pkt30 = pkt20;
1347                 pkt31 = pkt21;
1348                 pkt20 = pkt10;
1349                 pkt21 = pkt11;
1350                 pkt10 = pkt00;
1351                 pkt11 = pkt01;
1352                 q30 = q20;
1353                 q31 = q21;
1354                 q20 = q10;
1355                 q21 = q11;
1356                 q30_base = q20_base;
1357                 q31_base = q21_base;
1358
1359                 /* Stage 0: Get packets in */
1360                 pkt00 = pkts[i];
1361                 pkt01 = pkts[i + 1];
1362                 rte_prefetch0(pkt00);
1363                 rte_prefetch0(pkt01);
1364
1365                 /* Stage 1: Prefetch queue structure storing queue pointers */
1366                 q10 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt10);
1367                 q11 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt11);
1368
1369                 /* Stage 2: Prefetch queue write location */
1370                 q20_base = rte_sched_port_qbase(port, q20);
1371                 q21_base = rte_sched_port_qbase(port, q21);
1372                 rte_sched_port_enqueue_qwa_prefetch0(port, q20, q20_base);
1373                 rte_sched_port_enqueue_qwa_prefetch0(port, q21, q21_base);
1374
1375                 /* Stage 3: Write packet to queue and activate queue */
1376                 r30 = rte_sched_port_enqueue_qwa(port, q30, q30_base, pkt30);
1377                 r31 = rte_sched_port_enqueue_qwa(port, q31, q31_base, pkt31);
1378                 result += r30 + r31;
1379         }
1380
1381         /* Drain the pipeline (exactly 6 packets). Handle the last packet in the case
1382         of an odd number of input packets. */
1383         pkt_last = pkts[n_pkts - 1];
1384         rte_prefetch0(pkt_last);
1385
1386         q00 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt00);
1387         q01 = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt01);
1388
1389         q10_base = rte_sched_port_qbase(port, q10);
1390         q11_base = rte_sched_port_qbase(port, q11);
1391         rte_sched_port_enqueue_qwa_prefetch0(port, q10, q10_base);
1392         rte_sched_port_enqueue_qwa_prefetch0(port, q11, q11_base);
1393
1394         r20 = rte_sched_port_enqueue_qwa(port, q20, q20_base, pkt20);
1395         r21 = rte_sched_port_enqueue_qwa(port, q21, q21_base, pkt21);
1396         result += r20 + r21;
1397
1398         q_last = rte_sched_port_enqueue_qptrs_prefetch0(port, pkt_last);
1399
1400         q00_base = rte_sched_port_qbase(port, q00);
1401         q01_base = rte_sched_port_qbase(port, q01);
1402         rte_sched_port_enqueue_qwa_prefetch0(port, q00, q00_base);
1403         rte_sched_port_enqueue_qwa_prefetch0(port, q01, q01_base);
1404
1405         r10 = rte_sched_port_enqueue_qwa(port, q10, q10_base, pkt10);
1406         r11 = rte_sched_port_enqueue_qwa(port, q11, q11_base, pkt11);
1407         result += r10 + r11;
1408
1409         q_last_base = rte_sched_port_qbase(port, q_last);
1410         rte_sched_port_enqueue_qwa_prefetch0(port, q_last, q_last_base);
1411
1412         r00 = rte_sched_port_enqueue_qwa(port, q00, q00_base, pkt00);
1413         r01 = rte_sched_port_enqueue_qwa(port, q01, q01_base, pkt01);
1414         result += r00 + r01;
1415
1416         if (n_pkts & 1) {
1417                 r_last = rte_sched_port_enqueue_qwa(port, q_last, q_last_base, pkt_last);
1418                 result += r_last;
1419         }
1420
1421         return result;
1422 }
1423
1424 #endif /* RTE_SCHED_ENQUEUE */
1425
1426 #if RTE_SCHED_TS_CREDITS_UPDATE == 0
1427
1428 #define grinder_credits_update(port, pos)
1429
1430 #elif !defined(RTE_SCHED_SUBPORT_TC_OV)
1431
1432 static inline void
1433 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1434 {
1435         struct rte_sched_grinder *grinder = port->grinder + pos;
1436         struct rte_sched_subport *subport = grinder->subport;
1437         struct rte_sched_pipe *pipe = grinder->pipe;
1438         struct rte_sched_pipe_profile *params = grinder->pipe_params;
1439         uint64_t n_periods;
1440
1441         /* Subport TB */
1442         n_periods = (port->time - subport->tb_time) / subport->tb_period;
1443         subport->tb_credits += n_periods * subport->tb_credits_per_period;
1444         subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1445         subport->tb_time += n_periods * subport->tb_period;
1446
1447         /* Pipe TB */
1448         n_periods = (port->time - pipe->tb_time) / params->tb_period;
1449         pipe->tb_credits += n_periods * params->tb_credits_per_period;
1450         pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1451         pipe->tb_time += n_periods * params->tb_period;
1452
1453         /* Subport TCs */
1454         if (unlikely(port->time >= subport->tc_time)) {
1455                 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1456                 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1457                 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1458                 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1459                 subport->tc_time = port->time + subport->tc_period;
1460         }
1461
1462         /* Pipe TCs */
1463         if (unlikely(port->time >= pipe->tc_time)) {
1464                 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1465                 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1466                 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1467                 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1468                 pipe->tc_time = port->time + params->tc_period;
1469         }
1470 }
1471
1472 #else
1473
1474 static inline uint32_t
1475 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
1476 {
1477         struct rte_sched_grinder *grinder = port->grinder + pos;
1478         struct rte_sched_subport *subport = grinder->subport;
1479         uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
1480         uint32_t tc_ov_consumption_max;
1481         uint32_t tc_ov_wm = subport->tc_ov_wm;
1482
1483         if (subport->tc_ov == 0) {
1484                 return subport->tc_ov_wm_max;
1485         }
1486
1487         tc_ov_consumption[0] = subport->tc_credits_per_period[0] - subport->tc_credits[0];
1488         tc_ov_consumption[1] = subport->tc_credits_per_period[1] - subport->tc_credits[1];
1489         tc_ov_consumption[2] = subport->tc_credits_per_period[2] - subport->tc_credits[2];
1490         tc_ov_consumption[3] = subport->tc_credits_per_period[3] - subport->tc_credits[3];
1491
1492         tc_ov_consumption_max = subport->tc_credits_per_period[3] -
1493                 (tc_ov_consumption[0] + tc_ov_consumption[1] + tc_ov_consumption[2]);
1494
1495         if (tc_ov_consumption[3] > (tc_ov_consumption_max - port->mtu)) {
1496                 tc_ov_wm  -= tc_ov_wm >> 7;
1497                 if (tc_ov_wm < subport->tc_ov_wm_min) {
1498                         tc_ov_wm = subport->tc_ov_wm_min;
1499                 }
1500                 return tc_ov_wm;
1501         }
1502
1503         tc_ov_wm += (tc_ov_wm >> 7) + 1;
1504         if (tc_ov_wm > subport->tc_ov_wm_max) {
1505                 tc_ov_wm = subport->tc_ov_wm_max;
1506         }
1507         return tc_ov_wm;
1508 }
1509
1510 static inline void
1511 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
1512 {
1513         struct rte_sched_grinder *grinder = port->grinder + pos;
1514         struct rte_sched_subport *subport = grinder->subport;
1515         struct rte_sched_pipe *pipe = grinder->pipe;
1516         struct rte_sched_pipe_profile *params = grinder->pipe_params;
1517         uint64_t n_periods;
1518
1519         /* Subport TB */
1520         n_periods = (port->time - subport->tb_time) / subport->tb_period;
1521         subport->tb_credits += n_periods * subport->tb_credits_per_period;
1522         subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
1523         subport->tb_time += n_periods * subport->tb_period;
1524
1525         /* Pipe TB */
1526         n_periods = (port->time - pipe->tb_time) / params->tb_period;
1527         pipe->tb_credits += n_periods * params->tb_credits_per_period;
1528         pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
1529         pipe->tb_time += n_periods * params->tb_period;
1530
1531         /* Subport TCs */
1532         if (unlikely(port->time >= subport->tc_time)) {
1533                 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
1534
1535                 subport->tc_credits[0] = subport->tc_credits_per_period[0];
1536                 subport->tc_credits[1] = subport->tc_credits_per_period[1];
1537                 subport->tc_credits[2] = subport->tc_credits_per_period[2];
1538                 subport->tc_credits[3] = subport->tc_credits_per_period[3];
1539
1540                 subport->tc_time = port->time + subport->tc_period;
1541                 subport->tc_ov_period_id ++;
1542         }
1543
1544         /* Pipe TCs */
1545         if (unlikely(port->time >= pipe->tc_time)) {
1546                 pipe->tc_credits[0] = params->tc_credits_per_period[0];
1547                 pipe->tc_credits[1] = params->tc_credits_per_period[1];
1548                 pipe->tc_credits[2] = params->tc_credits_per_period[2];
1549                 pipe->tc_credits[3] = params->tc_credits_per_period[3];
1550                 pipe->tc_time = port->time + params->tc_period;
1551         }
1552
1553         /* Pipe TCs - Oversubscription */
1554         if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
1555                 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
1556
1557                 pipe->tc_ov_period_id = subport->tc_ov_period_id;
1558         }
1559 }
1560
1561 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
1562
1563 #if RTE_SCHED_TS_CREDITS_CHECK
1564
1565 #ifndef RTE_SCHED_SUBPORT_TC_OV
1566
1567 static inline int
1568 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1569 {
1570         struct rte_sched_grinder *grinder = port->grinder + pos;
1571         struct rte_sched_subport *subport = grinder->subport;
1572         struct rte_sched_pipe *pipe = grinder->pipe;
1573         struct rte_mbuf *pkt = grinder->pkt;
1574         uint32_t tc_index = grinder->tc_index;
1575         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1576         uint32_t subport_tb_credits = subport->tb_credits;
1577         uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1578         uint32_t pipe_tb_credits = pipe->tb_credits;
1579         uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1580         int enough_credits;
1581
1582         /* Check queue credits */
1583         enough_credits = (pkt_len <= subport_tb_credits) &&
1584                 (pkt_len <= subport_tc_credits) &&
1585                 (pkt_len <= pipe_tb_credits) &&
1586                 (pkt_len <= pipe_tc_credits);
1587
1588         if (!enough_credits) {
1589                 return 0;
1590         }
1591
1592         /* Update port credits */
1593         subport->tb_credits -= pkt_len;
1594         subport->tc_credits[tc_index] -= pkt_len;
1595         pipe->tb_credits -= pkt_len;
1596         pipe->tc_credits[tc_index] -= pkt_len;
1597
1598         return 1;
1599 }
1600
1601 #else
1602
1603 static inline int
1604 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
1605 {
1606         struct rte_sched_grinder *grinder = port->grinder + pos;
1607         struct rte_sched_subport *subport = grinder->subport;
1608         struct rte_sched_pipe *pipe = grinder->pipe;
1609         struct rte_mbuf *pkt = grinder->pkt;
1610         uint32_t tc_index = grinder->tc_index;
1611         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1612         uint32_t subport_tb_credits = subport->tb_credits;
1613         uint32_t subport_tc_credits = subport->tc_credits[tc_index];
1614         uint32_t pipe_tb_credits = pipe->tb_credits;
1615         uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
1616         uint32_t pipe_tc_ov_mask1[] = {UINT32_MAX, UINT32_MAX, UINT32_MAX, pipe->tc_ov_credits};
1617         uint32_t pipe_tc_ov_mask2[] = {0, 0, 0, UINT32_MAX};
1618         uint32_t pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
1619         int enough_credits;
1620
1621         /* Check pipe and subport credits */
1622         enough_credits = (pkt_len <= subport_tb_credits) &&
1623                 (pkt_len <= subport_tc_credits) &&
1624                 (pkt_len <= pipe_tb_credits) &&
1625                 (pkt_len <= pipe_tc_credits) &&
1626                 (pkt_len <= pipe_tc_ov_credits);
1627
1628         if (!enough_credits) {
1629                 return 0;
1630         }
1631
1632         /* Update pipe and subport credits */
1633         subport->tb_credits -= pkt_len;
1634         subport->tc_credits[tc_index] -= pkt_len;
1635         pipe->tb_credits -= pkt_len;
1636         pipe->tc_credits[tc_index] -= pkt_len;
1637         pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
1638
1639         return 1;
1640 }
1641
1642 #endif /* RTE_SCHED_SUBPORT_TC_OV */
1643
1644 #endif /* RTE_SCHED_TS_CREDITS_CHECK */
1645
1646 static inline int
1647 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
1648 {
1649         struct rte_sched_grinder *grinder = port->grinder + pos;
1650         struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
1651         struct rte_mbuf *pkt = grinder->pkt;
1652         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
1653
1654 #if RTE_SCHED_TS_CREDITS_CHECK
1655         if (!grinder_credits_check(port, pos)) {
1656                 return 0;
1657         }
1658 #endif
1659
1660         /* Advance port time */
1661         port->time += pkt_len;
1662
1663         /* Send packet */
1664         port->pkts_out[port->n_pkts_out ++] = pkt;
1665         queue->qr ++;
1666         grinder->wrr_tokens[grinder->qpos] += pkt_len * grinder->wrr_cost[grinder->qpos];
1667         if (queue->qr == queue->qw) {
1668                 uint32_t qindex = grinder->qindex[grinder->qpos];
1669
1670                 rte_bitmap_clear(port->bmp, qindex);
1671                 grinder->qmask &= ~(1 << grinder->qpos);
1672                 grinder->wrr_mask[grinder->qpos] = 0;
1673                 rte_sched_port_set_queue_empty_timestamp(port, qindex);
1674         }
1675
1676         /* Reset pipe loop detection */
1677         port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1678         grinder->productive = 1;
1679
1680         return 1;
1681 }
1682
1683 #if RTE_SCHED_OPTIMIZATIONS
1684
1685 static inline int
1686 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1687 {
1688         __m128i index = _mm_set1_epi32 (base_pipe);
1689         __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
1690         __m128i res = _mm_cmpeq_epi32(pipes, index);
1691         pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
1692         pipes = _mm_cmpeq_epi32(pipes, index);
1693         res = _mm_or_si128(res, pipes);
1694
1695         if (_mm_testz_si128(res, res))
1696                 return 0;
1697
1698         return 1;
1699 }
1700
1701 #else
1702
1703 static inline int
1704 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
1705 {
1706         uint32_t i;
1707
1708         for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i ++) {
1709                 if (port->grinder_base_bmp_pos[i] == base_pipe) {
1710                         return 1;
1711                 }
1712         }
1713
1714         return 0;
1715 }
1716
1717 #endif /* RTE_SCHED_OPTIMIZATIONS */
1718
1719 static inline void
1720 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
1721 {
1722         struct rte_sched_grinder *grinder = port->grinder + pos;
1723         uint16_t w[4];
1724
1725         grinder->pcache_w = 0;
1726         grinder->pcache_r = 0;
1727
1728         w[0] = (uint16_t) bmp_slab;
1729         w[1] = (uint16_t) (bmp_slab >> 16);
1730         w[2] = (uint16_t) (bmp_slab >> 32);
1731         w[3] = (uint16_t) (bmp_slab >> 48);
1732
1733         grinder->pcache_qmask[grinder->pcache_w] = w[0];
1734         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
1735         grinder->pcache_w += (w[0] != 0);
1736
1737         grinder->pcache_qmask[grinder->pcache_w] = w[1];
1738         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
1739         grinder->pcache_w += (w[1] != 0);
1740
1741         grinder->pcache_qmask[grinder->pcache_w] = w[2];
1742         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
1743         grinder->pcache_w += (w[2] != 0);
1744
1745         grinder->pcache_qmask[grinder->pcache_w] = w[3];
1746         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
1747         grinder->pcache_w += (w[3] != 0);
1748 }
1749
1750 static inline void
1751 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
1752 {
1753         struct rte_sched_grinder *grinder = port->grinder + pos;
1754         uint8_t b[4];
1755
1756         grinder->tccache_w = 0;
1757         grinder->tccache_r = 0;
1758
1759         b[0] = (uint8_t) (qmask & 0xF);
1760         b[1] = (uint8_t) ((qmask >> 4) & 0xF);
1761         b[2] = (uint8_t) ((qmask >> 8) & 0xF);
1762         b[3] = (uint8_t) ((qmask >> 12) & 0xF);
1763
1764         grinder->tccache_qmask[grinder->tccache_w] = b[0];
1765         grinder->tccache_qindex[grinder->tccache_w] = qindex;
1766         grinder->tccache_w += (b[0] != 0);
1767
1768         grinder->tccache_qmask[grinder->tccache_w] = b[1];
1769         grinder->tccache_qindex[grinder->tccache_w] = qindex + 4;
1770         grinder->tccache_w += (b[1] != 0);
1771
1772         grinder->tccache_qmask[grinder->tccache_w] = b[2];
1773         grinder->tccache_qindex[grinder->tccache_w] = qindex + 8;
1774         grinder->tccache_w += (b[2] != 0);
1775
1776         grinder->tccache_qmask[grinder->tccache_w] = b[3];
1777         grinder->tccache_qindex[grinder->tccache_w] = qindex + 12;
1778         grinder->tccache_w += (b[3] != 0);
1779 }
1780
1781 static inline int
1782 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
1783 {
1784         struct rte_sched_grinder *grinder = port->grinder + pos;
1785         struct rte_mbuf **qbase;
1786         uint32_t qindex;
1787         uint16_t qsize;
1788
1789         if (grinder->tccache_r == grinder->tccache_w) {
1790                 return 0;
1791         }
1792
1793         qindex = grinder->tccache_qindex[grinder->tccache_r];
1794         qbase = rte_sched_port_qbase(port, qindex);
1795         qsize = rte_sched_port_qsize(port, qindex);
1796
1797         grinder->tc_index = (qindex >> 2) & 0x3;
1798         grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
1799         grinder->qsize = qsize;
1800
1801         grinder->qindex[0] = qindex;
1802         grinder->qindex[1] = qindex + 1;
1803         grinder->qindex[2] = qindex + 2;
1804         grinder->qindex[3] = qindex + 3;
1805
1806         grinder->queue[0] = port->queue + qindex;
1807         grinder->queue[1] = port->queue + qindex + 1;
1808         grinder->queue[2] = port->queue + qindex + 2;
1809         grinder->queue[3] = port->queue + qindex + 3;
1810
1811         grinder->qbase[0] = qbase;
1812         grinder->qbase[1] = qbase + qsize;
1813         grinder->qbase[2] = qbase + 2 * qsize;
1814         grinder->qbase[3] = qbase + 3 * qsize;
1815
1816         grinder->tccache_r ++;
1817         return 1;
1818 }
1819
1820 static inline int
1821 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
1822 {
1823         struct rte_sched_grinder *grinder = port->grinder + pos;
1824         uint32_t pipe_qindex;
1825         uint16_t pipe_qmask;
1826
1827         if (grinder->pcache_r < grinder->pcache_w) {
1828                 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
1829                 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
1830                 grinder->pcache_r ++;
1831         } else {
1832                 uint64_t bmp_slab = 0;
1833                 uint32_t bmp_pos = 0;
1834
1835                 /* Get another non-empty pipe group */
1836                 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0)) {
1837                         return 0;
1838                 }
1839
1840 #if RTE_SCHED_DEBUG
1841                 debug_check_queue_slab(port, bmp_pos, bmp_slab);
1842 #endif
1843
1844                 /* Return if pipe group already in one of the other grinders */
1845                 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
1846                 if (unlikely(grinder_pipe_exists(port, bmp_pos))) {
1847                         return 0;
1848                 }
1849                 port->grinder_base_bmp_pos[pos] = bmp_pos;
1850
1851                 /* Install new pipe group into grinder's pipe cache */
1852                 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
1853
1854                 pipe_qmask = grinder->pcache_qmask[0];
1855                 pipe_qindex = grinder->pcache_qindex[0];
1856                 grinder->pcache_r = 1;
1857         }
1858
1859         /* Install new pipe in the grinder */
1860         grinder->pindex = pipe_qindex >> 4;
1861         grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
1862         grinder->pipe = port->pipe + grinder->pindex;
1863         grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
1864         grinder->productive = 0;
1865
1866         grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
1867         grinder_next_tc(port, pos);
1868
1869         /* Check for pipe exhaustion */
1870         if (grinder->pindex == port->pipe_loop) {
1871                 port->pipe_exhaustion = 1;
1872                 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
1873         }
1874
1875         return 1;
1876 }
1877
1878 #if RTE_SCHED_WRR == 0
1879
1880 #define grinder_wrr_load(a,b)
1881
1882 #define grinder_wrr_store(a,b)
1883
1884 static inline void
1885 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
1886 {
1887         struct rte_sched_grinder *grinder = port->grinder + pos;
1888         uint64_t slab = grinder->qmask;
1889
1890         if (rte_bsf64(slab, &grinder->qpos) == 0) {
1891                 rte_panic("grinder wrr\n");
1892         }
1893 }
1894
1895 #elif RTE_SCHED_WRR == 1
1896
1897 static inline void
1898 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
1899 {
1900         struct rte_sched_grinder *grinder = port->grinder + pos;
1901         struct rte_sched_pipe *pipe = grinder->pipe;
1902         struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
1903         uint32_t tc_index = grinder->tc_index;
1904         uint32_t qmask = grinder->qmask;
1905         uint32_t qindex;
1906
1907         qindex = tc_index * 4;
1908
1909         grinder->wrr_tokens[0] = ((uint16_t) pipe->wrr_tokens[qindex]) << RTE_SCHED_WRR_SHIFT;
1910         grinder->wrr_tokens[1] = ((uint16_t) pipe->wrr_tokens[qindex + 1]) << RTE_SCHED_WRR_SHIFT;
1911         grinder->wrr_tokens[2] = ((uint16_t) pipe->wrr_tokens[qindex + 2]) << RTE_SCHED_WRR_SHIFT;
1912         grinder->wrr_tokens[3] = ((uint16_t) pipe->wrr_tokens[qindex + 3]) << RTE_SCHED_WRR_SHIFT;
1913
1914         grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
1915         grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
1916         grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
1917         grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
1918
1919         grinder->wrr_cost[0] = pipe_params->wrr_cost[qindex];
1920         grinder->wrr_cost[1] = pipe_params->wrr_cost[qindex + 1];
1921         grinder->wrr_cost[2] = pipe_params->wrr_cost[qindex + 2];
1922         grinder->wrr_cost[3] = pipe_params->wrr_cost[qindex + 3];
1923 }
1924
1925 static inline void
1926 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
1927 {
1928         struct rte_sched_grinder *grinder = port->grinder + pos;
1929         struct rte_sched_pipe *pipe = grinder->pipe;
1930         uint32_t tc_index = grinder->tc_index;
1931         uint32_t qindex;
1932
1933         qindex = tc_index * 4;
1934
1935         pipe->wrr_tokens[qindex] = (uint8_t) ((grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >> RTE_SCHED_WRR_SHIFT);
1936         pipe->wrr_tokens[qindex + 1] = (uint8_t) ((grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >> RTE_SCHED_WRR_SHIFT);
1937         pipe->wrr_tokens[qindex + 2] = (uint8_t) ((grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >> RTE_SCHED_WRR_SHIFT);
1938         pipe->wrr_tokens[qindex + 3] = (uint8_t) ((grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >> RTE_SCHED_WRR_SHIFT);
1939 }
1940
1941 static inline void
1942 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
1943 {
1944         struct rte_sched_grinder *grinder = port->grinder + pos;
1945         uint16_t wrr_tokens_min;
1946
1947         grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
1948         grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
1949         grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
1950         grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
1951
1952         grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
1953         wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
1954
1955         grinder->wrr_tokens[0] -= wrr_tokens_min;
1956         grinder->wrr_tokens[1] -= wrr_tokens_min;
1957         grinder->wrr_tokens[2] -= wrr_tokens_min;
1958         grinder->wrr_tokens[3] -= wrr_tokens_min;
1959 }
1960
1961 #else
1962
1963 #error Invalid value for RTE_SCHED_WRR
1964
1965 #endif /* RTE_SCHED_WRR */
1966
1967 #define grinder_evict(port, pos)
1968
1969 static inline void
1970 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
1971 {
1972         struct rte_sched_grinder *grinder = port->grinder + pos;
1973
1974         rte_prefetch0(grinder->pipe);
1975         rte_prefetch0(grinder->queue[0]);
1976 }
1977
1978 static inline void
1979 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
1980 {
1981         struct rte_sched_grinder *grinder = port->grinder + pos;
1982         uint16_t qsize, qr[4];
1983
1984         qsize = grinder->qsize;
1985         qr[0] = grinder->queue[0]->qr & (qsize - 1);
1986         qr[1] = grinder->queue[1]->qr & (qsize - 1);
1987         qr[2] = grinder->queue[2]->qr & (qsize - 1);
1988         qr[3] = grinder->queue[3]->qr & (qsize - 1);
1989
1990         rte_prefetch0(grinder->qbase[0] + qr[0]);
1991         rte_prefetch0(grinder->qbase[1] + qr[1]);
1992
1993         grinder_wrr_load(port, pos);
1994         grinder_wrr(port, pos);
1995
1996         rte_prefetch0(grinder->qbase[2] + qr[2]);
1997         rte_prefetch0(grinder->qbase[3] + qr[3]);
1998 }
1999
2000 static inline void
2001 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2002 {
2003         struct rte_sched_grinder *grinder = port->grinder + pos;
2004         uint32_t qpos = grinder->qpos;
2005         struct rte_mbuf **qbase = grinder->qbase[qpos];
2006         uint16_t qsize = grinder->qsize;
2007         uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2008
2009         grinder->pkt = qbase[qr];
2010         rte_prefetch0(grinder->pkt);
2011
2012         if (unlikely((qr & 0x7) == 7)) {
2013                 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2014
2015                 rte_prefetch0(qbase + qr_next);
2016         }
2017 }
2018
2019 static inline uint32_t
2020 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2021 {
2022         struct rte_sched_grinder *grinder = port->grinder + pos;
2023
2024         switch (grinder->state) {
2025         case e_GRINDER_PREFETCH_PIPE:
2026         {
2027                 if (grinder_next_pipe(port, pos)) {
2028                         grinder_prefetch_pipe(port, pos);
2029                         port->busy_grinders ++;
2030
2031                         grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2032                         return 0;
2033                 }
2034
2035                 return 0;
2036         }
2037
2038         case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2039         {
2040                 struct rte_sched_pipe *pipe = grinder->pipe;
2041
2042                 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2043                 grinder_prefetch_tc_queue_arrays(port, pos);
2044                 grinder_credits_update(port, pos);
2045
2046                 grinder->state = e_GRINDER_PREFETCH_MBUF;
2047                 return 0;
2048         }
2049
2050         case e_GRINDER_PREFETCH_MBUF:
2051         {
2052                 grinder_prefetch_mbuf(port, pos);
2053
2054                 grinder->state = e_GRINDER_READ_MBUF;
2055                 return 0;
2056         }
2057
2058         case e_GRINDER_READ_MBUF:
2059         {
2060                 uint32_t result = 0;
2061
2062                 result = grinder_schedule(port, pos);
2063
2064                 /* Look for next packet within the same TC */
2065                 if (result && grinder->qmask) {
2066                         grinder_wrr(port, pos);
2067                         grinder_prefetch_mbuf(port, pos);
2068
2069                         return 1;
2070                 }
2071                 grinder_wrr_store(port, pos);
2072
2073                 /* Look for another active TC within same pipe */
2074                 if (grinder_next_tc(port, pos)) {
2075                         grinder_prefetch_tc_queue_arrays(port, pos);
2076
2077                         grinder->state = e_GRINDER_PREFETCH_MBUF;
2078                         return result;
2079                 }
2080                 if ((grinder->productive == 0) && (port->pipe_loop == RTE_SCHED_PIPE_INVALID)) {
2081                         port->pipe_loop = grinder->pindex;
2082                 }
2083                 grinder_evict(port, pos);
2084
2085                 /* Look for another active pipe */
2086                 if (grinder_next_pipe(port, pos)) {
2087                         grinder_prefetch_pipe(port, pos);
2088
2089                         grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2090                         return result;
2091                 }
2092
2093                 /* No active pipe found */
2094                 port->busy_grinders --;
2095
2096                 grinder->state = e_GRINDER_PREFETCH_PIPE;
2097                 return result;
2098         }
2099
2100         default:
2101                 rte_panic("Algorithmic error (invalid state)\n");
2102                 return 0;
2103         }
2104 }
2105
2106 static inline void
2107 rte_sched_port_time_resync(struct rte_sched_port *port)
2108 {
2109         uint64_t cycles = rte_get_tsc_cycles();
2110         uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2111         double bytes_diff = ((double) cycles_diff) / port->cycles_per_byte;
2112
2113         /* Advance port time */
2114         port->time_cpu_cycles = cycles;
2115         port->time_cpu_bytes += (uint64_t) bytes_diff;
2116         if (port->time < port->time_cpu_bytes) {
2117                 port->time = port->time_cpu_bytes;
2118         }
2119
2120         /* Reset pipe loop detection */
2121         port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2122 }
2123
2124 static inline int
2125 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2126 {
2127         int exceptions;
2128
2129         /* Check if any exception flag is set */
2130         exceptions = (second_pass && port->busy_grinders == 0) ||
2131                 (port->pipe_exhaustion == 1);
2132
2133         /* Clear exception flags */
2134         port->pipe_exhaustion = 0;
2135
2136         return exceptions;
2137 }
2138
2139 int
2140 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2141 {
2142         uint32_t i, count;
2143
2144         port->pkts_out = pkts;
2145         port->n_pkts_out = 0;
2146
2147         rte_sched_port_time_resync(port);
2148
2149         /* Take each queue in the grinder one step further */
2150         for (i = 0, count = 0; ; i ++)  {
2151                 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2152                 if ((count == n_pkts) ||
2153                     rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {
2154                         break;
2155                 }
2156         }
2157
2158         return count;
2159 }