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