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