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