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