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