sched: modify pkt enqueue for config flexibility
[dpdk.git] / lib / librte_sched / rte_sched.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #include <rte_common.h>
9 #include <rte_log.h>
10 #include <rte_memory.h>
11 #include <rte_malloc.h>
12 #include <rte_cycles.h>
13 #include <rte_prefetch.h>
14 #include <rte_branch_prediction.h>
15 #include <rte_mbuf.h>
16 #include <rte_bitmap.h>
17 #include <rte_reciprocal.h>
18
19 #include "rte_sched.h"
20 #include "rte_sched_common.h"
21 #include "rte_approx.h"
22
23 #ifdef __INTEL_COMPILER
24 #pragma warning(disable:2259) /* conversion may lose significant bits */
25 #endif
26
27 #ifdef RTE_SCHED_VECTOR
28 #include <rte_vect.h>
29
30 #ifdef RTE_ARCH_X86
31 #define SCHED_VECTOR_SSE4
32 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
33 #define SCHED_VECTOR_NEON
34 #endif
35
36 #endif
37
38 #define RTE_SCHED_TB_RATE_CONFIG_ERR          (1e-7)
39 #define RTE_SCHED_WRR_SHIFT                   3
40 #define RTE_SCHED_MAX_QUEUES_PER_TC           RTE_SCHED_BE_QUEUES_PER_PIPE
41 #define RTE_SCHED_GRINDER_PCACHE_SIZE         (64 / RTE_SCHED_QUEUES_PER_PIPE)
42 #define RTE_SCHED_PIPE_INVALID                UINT32_MAX
43 #define RTE_SCHED_BMP_POS_INVALID             UINT32_MAX
44
45 /* Scaling for cycles_per_byte calculation
46  * Chosen so that minimum rate is 480 bit/sec
47  */
48 #define RTE_SCHED_TIME_SHIFT                  8
49
50 struct rte_sched_pipe_profile {
51         /* Token bucket (TB) */
52         uint32_t tb_period;
53         uint32_t tb_credits_per_period;
54         uint32_t tb_size;
55
56         /* Pipe traffic classes */
57         uint32_t tc_period;
58         uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
59         uint8_t tc_ov_weight;
60
61         /* Pipe best-effort traffic class queues */
62         uint8_t  wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
63 };
64
65 struct rte_sched_pipe {
66         /* Token bucket (TB) */
67         uint64_t tb_time; /* time of last update */
68         uint32_t tb_credits;
69
70         /* Pipe profile and flags */
71         uint32_t profile;
72
73         /* Traffic classes (TCs) */
74         uint64_t tc_time; /* time of next update */
75         uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
76
77         /* Weighted Round Robin (WRR) */
78         uint8_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
79
80         /* TC oversubscription */
81         uint32_t tc_ov_credits;
82         uint8_t tc_ov_period_id;
83 } __rte_cache_aligned;
84
85 struct rte_sched_queue {
86         uint16_t qw;
87         uint16_t qr;
88 };
89
90 struct rte_sched_queue_extra {
91         struct rte_sched_queue_stats stats;
92 #ifdef RTE_SCHED_RED
93         struct rte_red red;
94 #endif
95 };
96
97 enum grinder_state {
98         e_GRINDER_PREFETCH_PIPE = 0,
99         e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS,
100         e_GRINDER_PREFETCH_MBUF,
101         e_GRINDER_READ_MBUF
102 };
103
104 struct rte_sched_grinder {
105         /* Pipe cache */
106         uint16_t pcache_qmask[RTE_SCHED_GRINDER_PCACHE_SIZE];
107         uint32_t pcache_qindex[RTE_SCHED_GRINDER_PCACHE_SIZE];
108         uint32_t pcache_w;
109         uint32_t pcache_r;
110
111         /* Current pipe */
112         enum grinder_state state;
113         uint32_t productive;
114         uint32_t pindex;
115         struct rte_sched_subport *subport;
116         struct rte_sched_pipe *pipe;
117         struct rte_sched_pipe_profile *pipe_params;
118
119         /* TC cache */
120         uint8_t tccache_qmask[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
121         uint32_t tccache_qindex[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
122         uint32_t tccache_w;
123         uint32_t tccache_r;
124
125         /* Current TC */
126         uint32_t tc_index;
127         struct rte_sched_queue *queue[RTE_SCHED_MAX_QUEUES_PER_TC];
128         struct rte_mbuf **qbase[RTE_SCHED_MAX_QUEUES_PER_TC];
129         uint32_t qindex[RTE_SCHED_MAX_QUEUES_PER_TC];
130         uint16_t qsize;
131         uint32_t qmask;
132         uint32_t qpos;
133         struct rte_mbuf *pkt;
134
135         /* WRR */
136         uint16_t wrr_tokens[RTE_SCHED_BE_QUEUES_PER_PIPE];
137         uint16_t wrr_mask[RTE_SCHED_BE_QUEUES_PER_PIPE];
138         uint8_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
139 };
140
141 struct rte_sched_subport {
142         /* Token bucket (TB) */
143         uint64_t tb_time; /* time of last update */
144         uint32_t tb_period;
145         uint32_t tb_credits_per_period;
146         uint32_t tb_size;
147         uint32_t tb_credits;
148
149         /* Traffic classes (TCs) */
150         uint64_t tc_time; /* time of next update */
151         uint32_t tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
152         uint32_t tc_credits[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
153         uint32_t tc_period;
154
155         /* TC oversubscription */
156         uint32_t tc_ov_wm;
157         uint32_t tc_ov_wm_min;
158         uint32_t tc_ov_wm_max;
159         uint8_t tc_ov_period_id;
160         uint8_t tc_ov;
161         uint32_t tc_ov_n;
162         double tc_ov_rate;
163
164         /* Statistics */
165         struct rte_sched_subport_stats stats;
166
167         /* Subport pipes */
168         uint32_t n_pipes_per_subport_enabled;
169         uint32_t n_pipe_profiles;
170         uint32_t n_max_pipe_profiles;
171
172         /* Pipe best-effort TC rate */
173         uint32_t pipe_tc_be_rate_max;
174
175         /* Pipe queues size */
176         uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
177
178 #ifdef RTE_SCHED_RED
179         struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
180 #endif
181
182         /* Scheduling loop detection */
183         uint32_t pipe_loop;
184         uint32_t pipe_exhaustion;
185
186         /* Bitmap */
187         struct rte_bitmap *bmp;
188         uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
189
190         /* Grinders */
191         struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
192         uint32_t busy_grinders;
193
194         /* Queue base calculation */
195         uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
196         uint32_t qsize_sum;
197
198         struct rte_sched_pipe *pipe;
199         struct rte_sched_queue *queue;
200         struct rte_sched_queue_extra *queue_extra;
201         struct rte_sched_pipe_profile *pipe_profiles;
202         uint8_t *bmp_array;
203         struct rte_mbuf **queue_array;
204         uint8_t memory[0] __rte_cache_aligned;
205 } __rte_cache_aligned;
206
207 struct rte_sched_port {
208         /* User parameters */
209         uint32_t n_subports_per_port;
210         uint32_t n_pipes_per_subport;
211         uint32_t n_pipes_per_subport_log2;
212         uint16_t pipe_queue[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
213         uint8_t pipe_tc[RTE_SCHED_QUEUES_PER_PIPE];
214         uint8_t tc_queue[RTE_SCHED_QUEUES_PER_PIPE];
215         uint32_t rate;
216         uint32_t mtu;
217         uint32_t frame_overhead;
218         int socket;
219         uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
220         uint32_t n_pipe_profiles;
221         uint32_t n_max_pipe_profiles;
222         uint32_t pipe_tc_be_rate_max;
223 #ifdef RTE_SCHED_RED
224         struct rte_red_config red_config[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
225 #endif
226
227         /* Timing */
228         uint64_t time_cpu_cycles;     /* Current CPU time measured in CPU cyles */
229         uint64_t time_cpu_bytes;      /* Current CPU time measured in bytes */
230         uint64_t time;                /* Current NIC TX time measured in bytes */
231         struct rte_reciprocal inv_cycles_per_byte; /* CPU cycles per byte */
232
233         /* Scheduling loop detection */
234         uint32_t pipe_loop;
235         uint32_t pipe_exhaustion;
236
237         /* Bitmap */
238         struct rte_bitmap *bmp;
239         uint32_t grinder_base_bmp_pos[RTE_SCHED_PORT_N_GRINDERS] __rte_aligned_16;
240
241         /* Grinders */
242         struct rte_sched_grinder grinder[RTE_SCHED_PORT_N_GRINDERS];
243         uint32_t busy_grinders;
244         struct rte_mbuf **pkts_out;
245         uint32_t n_pkts_out;
246
247         /* Queue base calculation */
248         uint32_t qsize_add[RTE_SCHED_QUEUES_PER_PIPE];
249         uint32_t qsize_sum;
250
251         /* Large data structures */
252         struct rte_sched_subport *subports[0];
253         struct rte_sched_subport *subport;
254         struct rte_sched_pipe *pipe;
255         struct rte_sched_queue *queue;
256         struct rte_sched_queue_extra *queue_extra;
257         struct rte_sched_pipe_profile *pipe_profiles;
258         uint8_t *bmp_array;
259         struct rte_mbuf **queue_array;
260         uint8_t memory[0] __rte_cache_aligned;
261 } __rte_cache_aligned;
262
263 enum rte_sched_port_array {
264         e_RTE_SCHED_PORT_ARRAY_SUBPORT = 0,
265         e_RTE_SCHED_PORT_ARRAY_PIPE,
266         e_RTE_SCHED_PORT_ARRAY_QUEUE,
267         e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA,
268         e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES,
269         e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY,
270         e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY,
271         e_RTE_SCHED_PORT_ARRAY_TOTAL,
272 };
273
274 enum rte_sched_subport_array {
275         e_RTE_SCHED_SUBPORT_ARRAY_PIPE = 0,
276         e_RTE_SCHED_SUBPORT_ARRAY_QUEUE,
277         e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA,
278         e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES,
279         e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY,
280         e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY,
281         e_RTE_SCHED_SUBPORT_ARRAY_TOTAL,
282 };
283
284 static inline uint32_t
285 rte_sched_subport_pipe_queues(struct rte_sched_subport *subport)
286 {
287         return RTE_SCHED_QUEUES_PER_PIPE * subport->n_pipes_per_subport_enabled;
288 }
289
290 static inline struct rte_mbuf **
291 rte_sched_subport_pipe_qbase(struct rte_sched_subport *subport, uint32_t qindex)
292 {
293         uint32_t pindex = qindex >> 4;
294         uint32_t qpos = qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1);
295
296         return (subport->queue_array + pindex *
297                 subport->qsize_sum + subport->qsize_add[qpos]);
298 }
299
300 static inline uint16_t
301 rte_sched_subport_pipe_qsize(struct rte_sched_port *port,
302 struct rte_sched_subport *subport, uint32_t qindex)
303 {
304         uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
305
306         return subport->qsize[tc];
307 }
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 inline struct rte_mbuf **
316 rte_sched_port_qbase(struct rte_sched_port *port, uint32_t qindex)
317 {
318         uint32_t pindex = qindex >> 4;
319         uint32_t qpos = qindex & 0xF;
320
321         return (port->queue_array + pindex *
322                 port->qsize_sum + port->qsize_add[qpos]);
323 }
324
325 static inline uint16_t
326 rte_sched_port_qsize(struct rte_sched_port *port, uint32_t qindex)
327 {
328         uint32_t tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
329
330         return port->qsize[tc];
331 }
332
333 static inline uint16_t
334 rte_sched_port_pipe_queue(struct rte_sched_port *port, uint32_t traffic_class)
335 {
336         uint16_t pipe_queue = port->pipe_queue[traffic_class];
337
338         return pipe_queue;
339 }
340
341 static inline uint8_t
342 rte_sched_port_pipe_tc(struct rte_sched_port *port, uint32_t qindex)
343 {
344         uint8_t pipe_tc = port->pipe_tc[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
345
346         return pipe_tc;
347 }
348
349 static inline uint8_t
350 rte_sched_port_tc_queue(struct rte_sched_port *port, uint32_t qindex)
351 {
352         uint8_t tc_queue = port->tc_queue[qindex & (RTE_SCHED_QUEUES_PER_PIPE - 1)];
353
354         return tc_queue;
355 }
356
357 static int
358 pipe_profile_check(struct rte_sched_pipe_params *params,
359         uint32_t rate, uint16_t *qsize)
360 {
361         uint32_t i;
362
363         /* Pipe parameters */
364         if (params == NULL) {
365                 RTE_LOG(ERR, SCHED,
366                         "%s: Incorrect value for parameter params\n", __func__);
367                 return -EINVAL;
368         }
369
370         /* TB rate: non-zero, not greater than port rate */
371         if (params->tb_rate == 0 ||
372                 params->tb_rate > rate) {
373                 RTE_LOG(ERR, SCHED,
374                         "%s: Incorrect value for tb rate\n", __func__);
375                 return -EINVAL;
376         }
377
378         /* TB size: non-zero */
379         if (params->tb_size == 0) {
380                 RTE_LOG(ERR, SCHED,
381                         "%s: Incorrect value for tb size\n", __func__);
382                 return -EINVAL;
383         }
384
385         /* TC rate: non-zero if qsize non-zero, less than pipe rate */
386         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
387                 if ((qsize[i] == 0 && params->tc_rate[i] != 0) ||
388                         (qsize[i] != 0 && (params->tc_rate[i] == 0 ||
389                         params->tc_rate[i] > params->tb_rate))) {
390                         RTE_LOG(ERR, SCHED,
391                                 "%s: Incorrect value for qsize or tc_rate\n", __func__);
392                         return -EINVAL;
393                 }
394         }
395
396         if (params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
397                 qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
398                 RTE_LOG(ERR, SCHED,
399                         "%s: Incorrect value for be traffic class rate\n", __func__);
400                 return -EINVAL;
401         }
402
403         /* TC period: non-zero */
404         if (params->tc_period == 0) {
405                 RTE_LOG(ERR, SCHED,
406                         "%s: Incorrect value for tc period\n", __func__);
407                 return -EINVAL;
408         }
409
410         /*  Best effort tc oversubscription weight: non-zero */
411         if (params->tc_ov_weight == 0) {
412                 RTE_LOG(ERR, SCHED,
413                         "%s: Incorrect value for tc ov weight\n", __func__);
414                 return -EINVAL;
415         }
416
417         /* Queue WRR weights: non-zero */
418         for (i = 0; i < RTE_SCHED_BE_QUEUES_PER_PIPE; i++) {
419                 if (params->wrr_weights[i] == 0) {
420                         RTE_LOG(ERR, SCHED,
421                                 "%s: Incorrect value for wrr weight\n", __func__);
422                         return -EINVAL;
423                 }
424         }
425
426         return 0;
427 }
428
429 static int
430 rte_sched_port_check_params(struct rte_sched_port_params *params)
431 {
432         if (params == NULL) {
433                 RTE_LOG(ERR, SCHED,
434                         "%s: Incorrect value for parameter params\n", __func__);
435                 return -EINVAL;
436         }
437
438         /* socket */
439         if (params->socket < 0) {
440                 RTE_LOG(ERR, SCHED,
441                         "%s: Incorrect value for socket id\n", __func__);
442                 return -EINVAL;
443         }
444
445         /* rate */
446         if (params->rate == 0) {
447                 RTE_LOG(ERR, SCHED,
448                         "%s: Incorrect value for rate\n", __func__);
449                 return -EINVAL;
450         }
451
452         /* mtu */
453         if (params->mtu == 0) {
454                 RTE_LOG(ERR, SCHED,
455                         "%s: Incorrect value for mtu\n", __func__);
456                 return -EINVAL;
457         }
458
459         /* n_subports_per_port: non-zero, limited to 16 bits, power of 2 */
460         if (params->n_subports_per_port == 0 ||
461             params->n_subports_per_port > 1u << 16 ||
462             !rte_is_power_of_2(params->n_subports_per_port)) {
463                 RTE_LOG(ERR, SCHED,
464                         "%s: Incorrect value for number of subports\n", __func__);
465                 return -EINVAL;
466         }
467
468         /* n_pipes_per_subport: non-zero, power of 2 */
469         if (params->n_pipes_per_subport == 0 ||
470             !rte_is_power_of_2(params->n_pipes_per_subport)) {
471                 RTE_LOG(ERR, SCHED,
472                         "%s: Incorrect value for maximum pipes number\n", __func__);
473                 return -EINVAL;
474         }
475
476         return 0;
477 }
478
479 static uint32_t
480 rte_sched_port_get_array_base(struct rte_sched_port_params *params, enum rte_sched_port_array array)
481 {
482         uint32_t n_subports_per_port = params->n_subports_per_port;
483         uint32_t n_pipes_per_subport = params->n_pipes_per_subport;
484         uint32_t n_pipes_per_port = n_pipes_per_subport * n_subports_per_port;
485         uint32_t n_queues_per_port = RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport * n_subports_per_port;
486
487         uint32_t size_subport = n_subports_per_port * sizeof(struct rte_sched_subport);
488         uint32_t size_pipe = n_pipes_per_port * sizeof(struct rte_sched_pipe);
489         uint32_t size_queue = n_queues_per_port * sizeof(struct rte_sched_queue);
490         uint32_t size_queue_extra
491                 = n_queues_per_port * sizeof(struct rte_sched_queue_extra);
492         uint32_t size_pipe_profiles
493                 = params->n_max_pipe_profiles * sizeof(struct rte_sched_pipe_profile);
494         uint32_t size_bmp_array = rte_bitmap_get_memory_footprint(n_queues_per_port);
495         uint32_t size_per_pipe_queue_array, size_queue_array;
496
497         uint32_t base, i;
498
499         size_per_pipe_queue_array = 0;
500         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
501                 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
502                         size_per_pipe_queue_array +=
503                                 params->qsize[i] * sizeof(struct rte_mbuf *);
504                 else
505                         size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
506                                 params->qsize[i] * sizeof(struct rte_mbuf *);
507         }
508         size_queue_array = n_pipes_per_port * size_per_pipe_queue_array;
509
510         base = 0;
511
512         if (array == e_RTE_SCHED_PORT_ARRAY_SUBPORT)
513                 return base;
514         base += RTE_CACHE_LINE_ROUNDUP(size_subport);
515
516         if (array == e_RTE_SCHED_PORT_ARRAY_PIPE)
517                 return base;
518         base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
519
520         if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE)
521                 return base;
522         base += RTE_CACHE_LINE_ROUNDUP(size_queue);
523
524         if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_EXTRA)
525                 return base;
526         base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
527
528         if (array == e_RTE_SCHED_PORT_ARRAY_PIPE_PROFILES)
529                 return base;
530         base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
531
532         if (array == e_RTE_SCHED_PORT_ARRAY_BMP_ARRAY)
533                 return base;
534         base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
535
536         if (array == e_RTE_SCHED_PORT_ARRAY_QUEUE_ARRAY)
537                 return base;
538         base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
539
540         return base;
541 }
542
543 static uint32_t
544 rte_sched_subport_get_array_base(struct rte_sched_subport_params *params,
545         enum rte_sched_subport_array array)
546 {
547         uint32_t n_pipes_per_subport = params->n_pipes_per_subport_enabled;
548         uint32_t n_subport_pipe_queues =
549                 RTE_SCHED_QUEUES_PER_PIPE * n_pipes_per_subport;
550
551         uint32_t size_pipe = n_pipes_per_subport * sizeof(struct rte_sched_pipe);
552         uint32_t size_queue =
553                 n_subport_pipe_queues * sizeof(struct rte_sched_queue);
554         uint32_t size_queue_extra
555                 = n_subport_pipe_queues * sizeof(struct rte_sched_queue_extra);
556         uint32_t size_pipe_profiles = params->n_max_pipe_profiles *
557                 sizeof(struct rte_sched_pipe_profile);
558         uint32_t size_bmp_array =
559                 rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
560         uint32_t size_per_pipe_queue_array, size_queue_array;
561
562         uint32_t base, i;
563
564         size_per_pipe_queue_array = 0;
565         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
566                 if (i < RTE_SCHED_TRAFFIC_CLASS_BE)
567                         size_per_pipe_queue_array +=
568                                 params->qsize[i] * sizeof(struct rte_mbuf *);
569                 else
570                         size_per_pipe_queue_array += RTE_SCHED_MAX_QUEUES_PER_TC *
571                                 params->qsize[i] * sizeof(struct rte_mbuf *);
572         }
573         size_queue_array = n_pipes_per_subport * size_per_pipe_queue_array;
574
575         base = 0;
576
577         if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE)
578                 return base;
579         base += RTE_CACHE_LINE_ROUNDUP(size_pipe);
580
581         if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE)
582                 return base;
583         base += RTE_CACHE_LINE_ROUNDUP(size_queue);
584
585         if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA)
586                 return base;
587         base += RTE_CACHE_LINE_ROUNDUP(size_queue_extra);
588
589         if (array == e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES)
590                 return base;
591         base += RTE_CACHE_LINE_ROUNDUP(size_pipe_profiles);
592
593         if (array == e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY)
594                 return base;
595         base += RTE_CACHE_LINE_ROUNDUP(size_bmp_array);
596
597         if (array == e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY)
598                 return base;
599         base += RTE_CACHE_LINE_ROUNDUP(size_queue_array);
600
601         return base;
602 }
603
604 static void
605 rte_sched_subport_config_qsize(struct rte_sched_subport *subport)
606 {
607         uint32_t i;
608
609         subport->qsize_add[0] = 0;
610
611         /* Strict prority traffic class */
612         for (i = 1; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
613                 subport->qsize_add[i] = subport->qsize_add[i-1] + subport->qsize[i-1];
614
615         /* Best-effort traffic class */
616         subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] =
617                 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE] +
618                 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
619         subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] =
620                 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 1] +
621                 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
622         subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] =
623                 subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 2] +
624                 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
625
626         subport->qsize_sum = subport->qsize_add[RTE_SCHED_TRAFFIC_CLASS_BE + 3] +
627                 subport->qsize[RTE_SCHED_TRAFFIC_CLASS_BE];
628 }
629
630 static void
631 rte_sched_port_log_pipe_profile(struct rte_sched_subport *subport, uint32_t i)
632 {
633         struct rte_sched_pipe_profile *p = subport->pipe_profiles + i;
634
635         RTE_LOG(DEBUG, SCHED, "Low level config for pipe profile %u:\n"
636                 "       Token bucket: period = %u, credits per period = %u, size = %u\n"
637                 "       Traffic classes: period = %u,\n"
638                 "       credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
639                 "       Best-effort traffic class oversubscription: weight = %hhu\n"
640                 "       WRR cost: [%hhu, %hhu, %hhu, %hhu]\n",
641                 i,
642
643                 /* Token bucket */
644                 p->tb_period,
645                 p->tb_credits_per_period,
646                 p->tb_size,
647
648                 /* Traffic classes */
649                 p->tc_period,
650                 p->tc_credits_per_period[0],
651                 p->tc_credits_per_period[1],
652                 p->tc_credits_per_period[2],
653                 p->tc_credits_per_period[3],
654                 p->tc_credits_per_period[4],
655                 p->tc_credits_per_period[5],
656                 p->tc_credits_per_period[6],
657                 p->tc_credits_per_period[7],
658                 p->tc_credits_per_period[8],
659                 p->tc_credits_per_period[9],
660                 p->tc_credits_per_period[10],
661                 p->tc_credits_per_period[11],
662                 p->tc_credits_per_period[12],
663
664                 /* Best-effort traffic class oversubscription */
665                 p->tc_ov_weight,
666
667                 /* WRR */
668                 p->wrr_cost[0], p->wrr_cost[1], p->wrr_cost[2], p->wrr_cost[3]);
669 }
670
671 static inline uint64_t
672 rte_sched_time_ms_to_bytes(uint32_t time_ms, uint32_t rate)
673 {
674         uint64_t time = time_ms;
675
676         time = (time * rate) / 1000;
677
678         return time;
679 }
680
681 static void
682 rte_sched_pipe_profile_convert(struct rte_sched_subport *subport,
683         struct rte_sched_pipe_params *src,
684         struct rte_sched_pipe_profile *dst,
685         uint32_t rate)
686 {
687         uint32_t wrr_cost[RTE_SCHED_BE_QUEUES_PER_PIPE];
688         uint32_t lcd1, lcd2, lcd;
689         uint32_t i;
690
691         /* Token Bucket */
692         if (src->tb_rate == rate) {
693                 dst->tb_credits_per_period = 1;
694                 dst->tb_period = 1;
695         } else {
696                 double tb_rate = (double) src->tb_rate
697                                 / (double) rate;
698                 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
699
700                 rte_approx(tb_rate, d,
701                         &dst->tb_credits_per_period, &dst->tb_period);
702         }
703
704         dst->tb_size = src->tb_size;
705
706         /* Traffic Classes */
707         dst->tc_period = rte_sched_time_ms_to_bytes(src->tc_period,
708                                                 rate);
709
710         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
711                 if (subport->qsize[i])
712                         dst->tc_credits_per_period[i]
713                                 = rte_sched_time_ms_to_bytes(src->tc_period,
714                                         src->tc_rate[i]);
715
716         dst->tc_ov_weight = src->tc_ov_weight;
717
718         /* WRR queues */
719         wrr_cost[0] = src->wrr_weights[0];
720         wrr_cost[1] = src->wrr_weights[1];
721         wrr_cost[2] = src->wrr_weights[2];
722         wrr_cost[3] = src->wrr_weights[3];
723
724         lcd1 = rte_get_lcd(wrr_cost[0], wrr_cost[1]);
725         lcd2 = rte_get_lcd(wrr_cost[2], wrr_cost[3]);
726         lcd = rte_get_lcd(lcd1, lcd2);
727
728         wrr_cost[0] = lcd / wrr_cost[0];
729         wrr_cost[1] = lcd / wrr_cost[1];
730         wrr_cost[2] = lcd / wrr_cost[2];
731         wrr_cost[3] = lcd / wrr_cost[3];
732
733         dst->wrr_cost[0] = (uint8_t) wrr_cost[0];
734         dst->wrr_cost[1] = (uint8_t) wrr_cost[1];
735         dst->wrr_cost[2] = (uint8_t) wrr_cost[2];
736         dst->wrr_cost[3] = (uint8_t) wrr_cost[3];
737 }
738
739 static void
740 rte_sched_subport_config_pipe_profile_table(struct rte_sched_subport *subport,
741         struct rte_sched_subport_params *params, uint32_t rate)
742 {
743         uint32_t i;
744
745         for (i = 0; i < subport->n_pipe_profiles; i++) {
746                 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
747                 struct rte_sched_pipe_profile *dst = subport->pipe_profiles + i;
748
749                 rte_sched_pipe_profile_convert(subport, src, dst, rate);
750                 rte_sched_port_log_pipe_profile(subport, i);
751         }
752
753         subport->pipe_tc_be_rate_max = 0;
754         for (i = 0; i < subport->n_pipe_profiles; i++) {
755                 struct rte_sched_pipe_params *src = params->pipe_profiles + i;
756                 uint32_t pipe_tc_be_rate = src->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
757
758                 if (subport->pipe_tc_be_rate_max < pipe_tc_be_rate)
759                         subport->pipe_tc_be_rate_max = pipe_tc_be_rate;
760         }
761 }
762
763 static int
764 rte_sched_subport_check_params(struct rte_sched_subport_params *params,
765         uint32_t n_max_pipes_per_subport,
766         uint32_t rate)
767 {
768         uint32_t i;
769
770         /* Check user parameters */
771         if (params == NULL) {
772                 RTE_LOG(ERR, SCHED,
773                         "%s: Incorrect value for parameter params\n", __func__);
774                 return -EINVAL;
775         }
776
777         if (params->tb_rate == 0 || params->tb_rate > rate) {
778                 RTE_LOG(ERR, SCHED,
779                         "%s: Incorrect value for tb rate\n", __func__);
780                 return -EINVAL;
781         }
782
783         if (params->tb_size == 0) {
784                 RTE_LOG(ERR, SCHED,
785                         "%s: Incorrect value for tb size\n", __func__);
786                 return -EINVAL;
787         }
788
789         /* qsize: if non-zero, power of 2,
790          * no bigger than 32K (due to 16-bit read/write pointers)
791          */
792         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
793                 uint16_t qsize = params->qsize[i];
794
795                 if (qsize != 0 && !rte_is_power_of_2(qsize)) {
796                         RTE_LOG(ERR, SCHED,
797                                 "%s: Incorrect value for qsize\n", __func__);
798                         return -EINVAL;
799                 }
800         }
801
802         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
803                 uint32_t tc_rate = params->tc_rate[i];
804                 uint16_t qsize = params->qsize[i];
805
806                 if ((qsize == 0 && tc_rate != 0) ||
807                         (qsize != 0 && tc_rate == 0) ||
808                         (tc_rate > params->tb_rate)) {
809                         RTE_LOG(ERR, SCHED,
810                                 "%s: Incorrect value for tc rate\n", __func__);
811                         return -EINVAL;
812                 }
813         }
814
815         if (params->qsize[RTE_SCHED_TRAFFIC_CLASS_BE] == 0 ||
816                 params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE] == 0) {
817                 RTE_LOG(ERR, SCHED,
818                         "%s: Incorrect qsize or tc rate(best effort)\n", __func__);
819                 return -EINVAL;
820         }
821
822         if (params->tc_period == 0) {
823                 RTE_LOG(ERR, SCHED,
824                         "%s: Incorrect value for tc period\n", __func__);
825                 return -EINVAL;
826         }
827
828         /* n_pipes_per_subport: non-zero, power of 2 */
829         if (params->n_pipes_per_subport_enabled == 0 ||
830                 params->n_pipes_per_subport_enabled > n_max_pipes_per_subport ||
831             !rte_is_power_of_2(params->n_pipes_per_subport_enabled)) {
832                 RTE_LOG(ERR, SCHED,
833                         "%s: Incorrect value for pipes number\n", __func__);
834                 return -EINVAL;
835         }
836
837         /* pipe_profiles and n_pipe_profiles */
838         if (params->pipe_profiles == NULL ||
839             params->n_pipe_profiles == 0 ||
840                 params->n_max_pipe_profiles == 0 ||
841                 params->n_pipe_profiles > params->n_max_pipe_profiles) {
842                 RTE_LOG(ERR, SCHED,
843                         "%s: Incorrect value for pipe profiles\n", __func__);
844                 return -EINVAL;
845         }
846
847         for (i = 0; i < params->n_pipe_profiles; i++) {
848                 struct rte_sched_pipe_params *p = params->pipe_profiles + i;
849                 int status;
850
851                 status = pipe_profile_check(p, rate, &params->qsize[0]);
852                 if (status != 0) {
853                         RTE_LOG(ERR, SCHED,
854                                 "%s: Pipe profile check failed(%d)\n", __func__, status);
855                         return -EINVAL;
856                 }
857         }
858
859         return 0;
860 }
861
862 uint32_t
863 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params)
864 {
865         uint32_t size0, size1;
866         int status;
867
868         status = rte_sched_port_check_params(params);
869         if (status != 0) {
870                 RTE_LOG(NOTICE, SCHED,
871                         "Port scheduler params check failed (%d)\n", status);
872
873                 return 0;
874         }
875
876         size0 = sizeof(struct rte_sched_port);
877         size1 = rte_sched_port_get_array_base(params,
878                         e_RTE_SCHED_PORT_ARRAY_TOTAL);
879
880         return size0 + size1;
881 }
882
883 struct rte_sched_port *
884 rte_sched_port_config(struct rte_sched_port_params *params)
885 {
886         struct rte_sched_port *port = NULL;
887         uint32_t size0, size1;
888         uint32_t cycles_per_byte;
889         uint32_t i, j;
890         int status;
891
892         status = rte_sched_port_check_params(params);
893         if (status != 0) {
894                 RTE_LOG(ERR, SCHED,
895                         "%s: Port scheduler params check failed (%d)\n",
896                         __func__, status);
897                 return NULL;
898         }
899
900         size0 = sizeof(struct rte_sched_port);
901         size1 = params->n_subports_per_port * sizeof(struct rte_sched_subport *);
902
903         /* Allocate memory to store the data structures */
904         port = rte_zmalloc_socket("qos_params", size0 + size1, RTE_CACHE_LINE_SIZE,
905                 params->socket);
906         if (port == NULL) {
907                 RTE_LOG(ERR, SCHED, "%s: Memory allocation fails\n", __func__);
908
909                 return NULL;
910         }
911
912         /* User parameters */
913         port->n_subports_per_port = params->n_subports_per_port;
914         port->n_pipes_per_subport = params->n_pipes_per_subport;
915         port->n_pipes_per_subport_log2 =
916                         __builtin_ctz(params->n_pipes_per_subport);
917         port->socket = params->socket;
918
919         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
920                 port->pipe_queue[i] = i;
921
922         for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
923                 port->pipe_tc[i] = j;
924
925                 if (j < RTE_SCHED_TRAFFIC_CLASS_BE)
926                         j++;
927         }
928
929         for (i = 0, j = 0; i < RTE_SCHED_QUEUES_PER_PIPE; i++) {
930                 port->tc_queue[i] = j;
931
932                 if (i >= RTE_SCHED_TRAFFIC_CLASS_BE)
933                         j++;
934         }
935         port->rate = params->rate;
936         port->mtu = params->mtu + params->frame_overhead;
937         port->frame_overhead = params->frame_overhead;
938
939         /* Timing */
940         port->time_cpu_cycles = rte_get_tsc_cycles();
941         port->time_cpu_bytes = 0;
942         port->time = 0;
943
944         cycles_per_byte = (rte_get_tsc_hz() << RTE_SCHED_TIME_SHIFT)
945                 / params->rate;
946         port->inv_cycles_per_byte = rte_reciprocal_value(cycles_per_byte);
947
948         /* Grinders */
949         port->pkts_out = NULL;
950         port->n_pkts_out = 0;
951
952         return port;
953 }
954
955 static inline void
956 rte_sched_subport_free(struct rte_sched_port *port,
957         struct rte_sched_subport *subport)
958 {
959         uint32_t n_subport_pipe_queues;
960         uint32_t qindex;
961
962         if (subport == NULL)
963                 return;
964
965         n_subport_pipe_queues = rte_sched_subport_pipe_queues(subport);
966
967         /* Free enqueued mbufs */
968         for (qindex = 0; qindex < n_subport_pipe_queues; qindex++) {
969                 struct rte_mbuf **mbufs =
970                         rte_sched_subport_pipe_qbase(subport, qindex);
971                 uint16_t qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
972                 if (qsize != 0) {
973                         struct rte_sched_queue *queue = subport->queue + qindex;
974                         uint16_t qr = queue->qr & (qsize - 1);
975                         uint16_t qw = queue->qw & (qsize - 1);
976
977                         for (; qr != qw; qr = (qr + 1) & (qsize - 1))
978                                 rte_pktmbuf_free(mbufs[qr]);
979                 }
980         }
981
982         rte_bitmap_free(subport->bmp);
983 }
984
985 void
986 rte_sched_port_free(struct rte_sched_port *port)
987 {
988         uint32_t i;
989
990         /* Check user parameters */
991         if (port == NULL)
992                 return;
993
994         for (i = 0; i < port->n_subports_per_port; i++)
995                 rte_sched_subport_free(port, port->subports[i]);
996
997         rte_free(port);
998 }
999
1000 static void
1001 rte_sched_port_log_subport_config(struct rte_sched_port *port, uint32_t i)
1002 {
1003         struct rte_sched_subport *s = port->subports[i];
1004
1005         RTE_LOG(DEBUG, SCHED, "Low level config for subport %u:\n"
1006                 "       Token bucket: period = %u, credits per period = %u, size = %u\n"
1007                 "       Traffic classes: period = %u\n"
1008                 "       credits per period = [%u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u, %u]\n"
1009                 "       Best effort traffic class oversubscription: wm min = %u, wm max = %u\n",
1010                 i,
1011
1012                 /* Token bucket */
1013                 s->tb_period,
1014                 s->tb_credits_per_period,
1015                 s->tb_size,
1016
1017                 /* Traffic classes */
1018                 s->tc_period,
1019                 s->tc_credits_per_period[0],
1020                 s->tc_credits_per_period[1],
1021                 s->tc_credits_per_period[2],
1022                 s->tc_credits_per_period[3],
1023                 s->tc_credits_per_period[4],
1024                 s->tc_credits_per_period[5],
1025                 s->tc_credits_per_period[6],
1026                 s->tc_credits_per_period[7],
1027                 s->tc_credits_per_period[8],
1028                 s->tc_credits_per_period[9],
1029                 s->tc_credits_per_period[10],
1030                 s->tc_credits_per_period[11],
1031                 s->tc_credits_per_period[12],
1032
1033                 /* Best effort traffic class oversubscription */
1034                 s->tc_ov_wm_min,
1035                 s->tc_ov_wm_max);
1036 }
1037
1038 static void
1039 rte_sched_free_memory(struct rte_sched_port *port, uint32_t n_subports)
1040 {
1041         uint32_t i;
1042
1043         for (i = 0; i < n_subports; i++) {
1044                 struct rte_sched_subport *subport = port->subports[i];
1045
1046                 rte_sched_subport_free(port, subport);
1047         }
1048
1049         rte_free(port);
1050 }
1051
1052 int
1053 rte_sched_subport_config(struct rte_sched_port *port,
1054         uint32_t subport_id,
1055         struct rte_sched_subport_params *params)
1056 {
1057         struct rte_sched_subport *s = NULL;
1058         uint32_t n_subports = subport_id;
1059         uint32_t n_subport_pipe_queues, i;
1060         uint32_t size0, size1, bmp_mem_size;
1061         int status;
1062
1063         /* Check user parameters */
1064         if (port == NULL) {
1065                 RTE_LOG(ERR, SCHED,
1066                         "%s: Incorrect value for parameter port\n", __func__);
1067                 return 0;
1068         }
1069
1070         if (subport_id >= port->n_subports_per_port) {
1071                 RTE_LOG(ERR, SCHED,
1072                         "%s: Incorrect value for subport id\n", __func__);
1073
1074                 rte_sched_free_memory(port, n_subports);
1075                 return -EINVAL;
1076         }
1077
1078         status = rte_sched_subport_check_params(params,
1079                 port->n_pipes_per_subport,
1080                 port->rate);
1081         if (status != 0) {
1082                 RTE_LOG(NOTICE, SCHED,
1083                         "%s: Port scheduler params check failed (%d)\n",
1084                         __func__, status);
1085
1086                 rte_sched_free_memory(port, n_subports);
1087                 return -EINVAL;
1088         }
1089
1090         /* Determine the amount of memory to allocate */
1091         size0 = sizeof(struct rte_sched_subport);
1092         size1 = rte_sched_subport_get_array_base(params,
1093                                 e_RTE_SCHED_SUBPORT_ARRAY_TOTAL);
1094
1095         /* Allocate memory to store the data structures */
1096         s = rte_zmalloc_socket("subport_params", size0 + size1,
1097                 RTE_CACHE_LINE_SIZE, port->socket);
1098         if (s == NULL) {
1099                 RTE_LOG(ERR, SCHED,
1100                         "%s: Memory allocation fails\n", __func__);
1101
1102                 rte_sched_free_memory(port, n_subports);
1103                 return -ENOMEM;
1104         }
1105
1106         n_subports++;
1107
1108         /* Port */
1109         port->subports[subport_id] = s;
1110
1111         /* Token Bucket (TB) */
1112         if (params->tb_rate == port->rate) {
1113                 s->tb_credits_per_period = 1;
1114                 s->tb_period = 1;
1115         } else {
1116                 double tb_rate = ((double) params->tb_rate) / ((double) port->rate);
1117                 double d = RTE_SCHED_TB_RATE_CONFIG_ERR;
1118
1119                 rte_approx(tb_rate, d, &s->tb_credits_per_period, &s->tb_period);
1120         }
1121
1122         s->tb_size = params->tb_size;
1123         s->tb_time = port->time;
1124         s->tb_credits = s->tb_size / 2;
1125
1126         /* Traffic Classes (TCs) */
1127         s->tc_period = rte_sched_time_ms_to_bytes(params->tc_period, port->rate);
1128         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1129                 if (params->qsize[i])
1130                         s->tc_credits_per_period[i]
1131                                 = rte_sched_time_ms_to_bytes(params->tc_period,
1132                                         params->tc_rate[i]);
1133         }
1134         s->tc_time = port->time + s->tc_period;
1135         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1136                 if (params->qsize[i])
1137                         s->tc_credits[i] = s->tc_credits_per_period[i];
1138
1139         /* compile time checks */
1140         RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS == 0);
1141         RTE_BUILD_BUG_ON(RTE_SCHED_PORT_N_GRINDERS &
1142                 (RTE_SCHED_PORT_N_GRINDERS - 1));
1143
1144         /* User parameters */
1145         s->n_pipes_per_subport_enabled = params->n_pipes_per_subport_enabled;
1146         memcpy(s->qsize, params->qsize, sizeof(params->qsize));
1147         s->n_pipe_profiles = params->n_pipe_profiles;
1148         s->n_max_pipe_profiles = params->n_max_pipe_profiles;
1149
1150 #ifdef RTE_SCHED_RED
1151         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++) {
1152                 uint32_t j;
1153
1154                 for (j = 0; j < RTE_COLORS; j++) {
1155                         /* if min/max are both zero, then RED is disabled */
1156                         if ((params->red_params[i][j].min_th |
1157                              params->red_params[i][j].max_th) == 0) {
1158                                 continue;
1159                         }
1160
1161                         if (rte_red_config_init(&s->red_config[i][j],
1162                                 params->red_params[i][j].wq_log2,
1163                                 params->red_params[i][j].min_th,
1164                                 params->red_params[i][j].max_th,
1165                                 params->red_params[i][j].maxp_inv) != 0) {
1166                                 rte_sched_free_memory(port, n_subports);
1167
1168                                 RTE_LOG(NOTICE, SCHED,
1169                                 "%s: RED configuration init fails\n", __func__);
1170                                 return -EINVAL;
1171                         }
1172                 }
1173         }
1174 #endif
1175
1176         /* Scheduling loop detection */
1177         s->pipe_loop = RTE_SCHED_PIPE_INVALID;
1178         s->pipe_exhaustion = 0;
1179
1180         /* Grinders */
1181         s->busy_grinders = 0;
1182
1183         /* Queue base calculation */
1184         rte_sched_subport_config_qsize(s);
1185
1186         /* Large data structures */
1187         s->pipe = (struct rte_sched_pipe *)
1188                 (s->memory + rte_sched_subport_get_array_base(params,
1189                 e_RTE_SCHED_SUBPORT_ARRAY_PIPE));
1190         s->queue = (struct rte_sched_queue *)
1191                 (s->memory + rte_sched_subport_get_array_base(params,
1192                 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE));
1193         s->queue_extra = (struct rte_sched_queue_extra *)
1194                 (s->memory + rte_sched_subport_get_array_base(params,
1195                 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_EXTRA));
1196         s->pipe_profiles = (struct rte_sched_pipe_profile *)
1197                 (s->memory + rte_sched_subport_get_array_base(params,
1198                 e_RTE_SCHED_SUBPORT_ARRAY_PIPE_PROFILES));
1199         s->bmp_array =  s->memory + rte_sched_subport_get_array_base(params,
1200                 e_RTE_SCHED_SUBPORT_ARRAY_BMP_ARRAY);
1201         s->queue_array = (struct rte_mbuf **)
1202                 (s->memory + rte_sched_subport_get_array_base(params,
1203                 e_RTE_SCHED_SUBPORT_ARRAY_QUEUE_ARRAY));
1204
1205         /* Pipe profile table */
1206         rte_sched_subport_config_pipe_profile_table(s, params, port->rate);
1207
1208         /* Bitmap */
1209         n_subport_pipe_queues = rte_sched_subport_pipe_queues(s);
1210         bmp_mem_size = rte_bitmap_get_memory_footprint(n_subport_pipe_queues);
1211         s->bmp = rte_bitmap_init(n_subport_pipe_queues, s->bmp_array,
1212                                 bmp_mem_size);
1213         if (s->bmp == NULL) {
1214                 RTE_LOG(ERR, SCHED,
1215                         "%s: Subport bitmap init error\n", __func__);
1216
1217                 rte_sched_free_memory(port, n_subports);
1218                 return -EINVAL;
1219         }
1220
1221         for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++)
1222                 s->grinder_base_bmp_pos[i] = RTE_SCHED_PIPE_INVALID;
1223
1224 #ifdef RTE_SCHED_SUBPORT_TC_OV
1225         /* TC oversubscription */
1226         s->tc_ov_wm_min = port->mtu;
1227         s->tc_ov_wm_max = rte_sched_time_ms_to_bytes(params->tc_period,
1228                                                      s->pipe_tc_be_rate_max);
1229         s->tc_ov_wm = s->tc_ov_wm_max;
1230         s->tc_ov_period_id = 0;
1231         s->tc_ov = 0;
1232         s->tc_ov_n = 0;
1233         s->tc_ov_rate = 0;
1234 #endif
1235
1236         rte_sched_port_log_subport_config(port, subport_id);
1237
1238         return 0;
1239 }
1240
1241 int
1242 rte_sched_pipe_config(struct rte_sched_port *port,
1243         uint32_t subport_id,
1244         uint32_t pipe_id,
1245         int32_t pipe_profile)
1246 {
1247         struct rte_sched_subport *s;
1248         struct rte_sched_pipe *p;
1249         struct rte_sched_pipe_profile *params;
1250         uint32_t n_subports = subport_id + 1;
1251         uint32_t deactivate, profile, i;
1252
1253         /* Check user parameters */
1254         profile = (uint32_t) pipe_profile;
1255         deactivate = (pipe_profile < 0);
1256
1257         if (port == NULL) {
1258                 RTE_LOG(ERR, SCHED,
1259                         "%s: Incorrect value for parameter port\n", __func__);
1260                 return -EINVAL;
1261         }
1262
1263         if (subport_id >= port->n_subports_per_port) {
1264                 RTE_LOG(ERR, SCHED,
1265                         "%s: Incorrect value for parameter subport id\n", __func__);
1266
1267                 rte_sched_free_memory(port, n_subports);
1268                 return -EINVAL;
1269         }
1270
1271         s = port->subports[subport_id];
1272         if (pipe_id >= s->n_pipes_per_subport_enabled) {
1273                 RTE_LOG(ERR, SCHED,
1274                         "%s: Incorrect value for parameter pipe id\n", __func__);
1275
1276                 rte_sched_free_memory(port, n_subports);
1277                 return -EINVAL;
1278         }
1279
1280         if (!deactivate && profile >= s->n_pipe_profiles) {
1281                 RTE_LOG(ERR, SCHED,
1282                         "%s: Incorrect value for parameter pipe profile\n", __func__);
1283
1284                 rte_sched_free_memory(port, n_subports);
1285                 return -EINVAL;
1286         }
1287
1288         /* Handle the case when pipe already has a valid configuration */
1289         p = s->pipe + pipe_id;
1290         if (p->tb_time) {
1291                 params = s->pipe_profiles + p->profile;
1292
1293                 double subport_tc_be_rate =
1294                         (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1295                         / (double) s->tc_period;
1296                 double pipe_tc_be_rate =
1297                         (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1298                         / (double) params->tc_period;
1299                 uint32_t tc_be_ov = s->tc_ov;
1300
1301                 /* Unplug pipe from its subport */
1302                 s->tc_ov_n -= params->tc_ov_weight;
1303                 s->tc_ov_rate -= pipe_tc_be_rate;
1304                 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1305
1306                 if (s->tc_ov != tc_be_ov) {
1307                         RTE_LOG(DEBUG, SCHED,
1308                                 "Subport %u Best-effort TC oversubscription is OFF (%.4lf >= %.4lf)\n",
1309                                 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1310                 }
1311
1312                 /* Reset the pipe */
1313                 memset(p, 0, sizeof(struct rte_sched_pipe));
1314         }
1315
1316         if (deactivate)
1317                 return 0;
1318
1319         /* Apply the new pipe configuration */
1320         p->profile = profile;
1321         params = s->pipe_profiles + p->profile;
1322
1323         /* Token Bucket (TB) */
1324         p->tb_time = port->time;
1325         p->tb_credits = params->tb_size / 2;
1326
1327         /* Traffic Classes (TCs) */
1328         p->tc_time = port->time + params->tc_period;
1329
1330         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
1331                 if (s->qsize[i])
1332                         p->tc_credits[i] = params->tc_credits_per_period[i];
1333
1334         {
1335                 /* Subport best effort tc oversubscription */
1336                 double subport_tc_be_rate =
1337                         (double) s->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1338                         / (double) s->tc_period;
1339                 double pipe_tc_be_rate =
1340                         (double) params->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE]
1341                         / (double) params->tc_period;
1342                 uint32_t tc_be_ov = s->tc_ov;
1343
1344                 s->tc_ov_n += params->tc_ov_weight;
1345                 s->tc_ov_rate += pipe_tc_be_rate;
1346                 s->tc_ov = s->tc_ov_rate > subport_tc_be_rate;
1347
1348                 if (s->tc_ov != tc_be_ov) {
1349                         RTE_LOG(DEBUG, SCHED,
1350                                 "Subport %u Best effort TC oversubscription is ON (%.4lf < %.4lf)\n",
1351                                 subport_id, subport_tc_be_rate, s->tc_ov_rate);
1352                 }
1353                 p->tc_ov_period_id = s->tc_ov_period_id;
1354                 p->tc_ov_credits = s->tc_ov_wm;
1355         }
1356
1357         return 0;
1358 }
1359
1360 int
1361 rte_sched_subport_pipe_profile_add(struct rte_sched_port *port,
1362         uint32_t subport_id,
1363         struct rte_sched_pipe_params *params,
1364         uint32_t *pipe_profile_id)
1365 {
1366         struct rte_sched_subport *s;
1367         struct rte_sched_pipe_profile *pp;
1368         uint32_t i;
1369         int status;
1370
1371         /* Port */
1372         if (port == NULL) {
1373                 RTE_LOG(ERR, SCHED,
1374                         "%s: Incorrect value for parameter port\n", __func__);
1375                 return -EINVAL;
1376         }
1377
1378         /* Subport id not exceeds the max limit */
1379         if (subport_id > port->n_subports_per_port) {
1380                 RTE_LOG(ERR, SCHED,
1381                         "%s: Incorrect value for subport id\n", __func__);
1382                 return -EINVAL;
1383         }
1384
1385         s = port->subports[subport_id];
1386
1387         /* Pipe profiles exceeds the max limit */
1388         if (s->n_pipe_profiles >= s->n_max_pipe_profiles) {
1389                 RTE_LOG(ERR, SCHED,
1390                         "%s: Number of pipe profiles exceeds the max limit\n", __func__);
1391                 return -EINVAL;
1392         }
1393
1394         /* Pipe params */
1395         status = pipe_profile_check(params, port->rate, &s->qsize[0]);
1396         if (status != 0) {
1397                 RTE_LOG(ERR, SCHED,
1398                         "%s: Pipe profile check failed(%d)\n", __func__, status);
1399                 return -EINVAL;
1400         }
1401
1402         pp = &s->pipe_profiles[s->n_pipe_profiles];
1403         rte_sched_pipe_profile_convert(s, params, pp, port->rate);
1404
1405         /* Pipe profile should not exists */
1406         for (i = 0; i < s->n_pipe_profiles; i++)
1407                 if (memcmp(s->pipe_profiles + i, pp, sizeof(*pp)) == 0) {
1408                         RTE_LOG(ERR, SCHED,
1409                                 "%s: Pipe profile exists\n", __func__);
1410                         return -EINVAL;
1411                 }
1412
1413         /* Pipe profile commit */
1414         *pipe_profile_id = s->n_pipe_profiles;
1415         s->n_pipe_profiles++;
1416
1417         if (s->pipe_tc_be_rate_max < params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE])
1418                 s->pipe_tc_be_rate_max = params->tc_rate[RTE_SCHED_TRAFFIC_CLASS_BE];
1419
1420         rte_sched_port_log_pipe_profile(s, *pipe_profile_id);
1421
1422         return 0;
1423 }
1424
1425 static inline uint32_t
1426 rte_sched_port_qindex(struct rte_sched_port *port,
1427         uint32_t subport,
1428         uint32_t pipe,
1429         uint32_t traffic_class,
1430         uint32_t queue)
1431 {
1432         return ((subport & (port->n_subports_per_port - 1)) <<
1433                 (port->n_pipes_per_subport_log2 + 4)) |
1434                 ((pipe &
1435                 (port->subports[subport]->n_pipes_per_subport_enabled - 1)) << 4) |
1436                 ((rte_sched_port_pipe_queue(port, traffic_class) + queue) &
1437                 (RTE_SCHED_QUEUES_PER_PIPE - 1));
1438 }
1439
1440 void
1441 rte_sched_port_pkt_write(struct rte_sched_port *port,
1442                          struct rte_mbuf *pkt,
1443                          uint32_t subport, uint32_t pipe,
1444                          uint32_t traffic_class,
1445                          uint32_t queue, enum rte_color color)
1446 {
1447         uint32_t queue_id =
1448                 rte_sched_port_qindex(port, subport, pipe, traffic_class, queue);
1449
1450         rte_mbuf_sched_set(pkt, queue_id, traffic_class, (uint8_t)color);
1451 }
1452
1453 void
1454 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
1455                                   const struct rte_mbuf *pkt,
1456                                   uint32_t *subport, uint32_t *pipe,
1457                                   uint32_t *traffic_class, uint32_t *queue)
1458 {
1459         uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1460
1461         *subport = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1462         *pipe = (queue_id >> 4) &
1463                 (port->subports[*subport]->n_pipes_per_subport_enabled - 1);
1464         *traffic_class = rte_sched_port_pipe_tc(port, queue_id);
1465         *queue = rte_sched_port_tc_queue(port, queue_id);
1466 }
1467
1468 enum rte_color
1469 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt)
1470 {
1471         return (enum rte_color)rte_mbuf_sched_color_get(pkt);
1472 }
1473
1474 int
1475 rte_sched_subport_read_stats(struct rte_sched_port *port,
1476                              uint32_t subport_id,
1477                              struct rte_sched_subport_stats *stats,
1478                              uint32_t *tc_ov)
1479 {
1480         struct rte_sched_subport *s;
1481
1482         /* Check user parameters */
1483         if (port == NULL) {
1484                 RTE_LOG(ERR, SCHED,
1485                         "%s: Incorrect value for parameter port\n", __func__);
1486                 return -EINVAL;
1487         }
1488
1489         if (subport_id >= port->n_subports_per_port) {
1490                 RTE_LOG(ERR, SCHED,
1491                         "%s: Incorrect value for subport id\n", __func__);
1492                 return -EINVAL;
1493         }
1494
1495         if (stats == NULL) {
1496                 RTE_LOG(ERR, SCHED,
1497                         "%s: Incorrect value for parameter stats\n", __func__);
1498                 return -EINVAL;
1499         }
1500
1501         if (tc_ov == NULL) {
1502                 RTE_LOG(ERR, SCHED,
1503                         "%s: Incorrect value for tc_ov\n", __func__);
1504                 return -EINVAL;
1505         }
1506
1507         s = port->subports[subport_id];
1508
1509         /* Copy subport stats and clear */
1510         memcpy(stats, &s->stats, sizeof(struct rte_sched_subport_stats));
1511         memset(&s->stats, 0, sizeof(struct rte_sched_subport_stats));
1512
1513         /* Subport TC oversubscription status */
1514         *tc_ov = s->tc_ov;
1515
1516         return 0;
1517 }
1518
1519 int
1520 rte_sched_queue_read_stats(struct rte_sched_port *port,
1521         uint32_t queue_id,
1522         struct rte_sched_queue_stats *stats,
1523         uint16_t *qlen)
1524 {
1525         struct rte_sched_queue *q;
1526         struct rte_sched_queue_extra *qe;
1527
1528         /* Check user parameters */
1529         if (port == NULL) {
1530                 RTE_LOG(ERR, SCHED,
1531                         "%s: Incorrect value for parameter port\n", __func__);
1532                 return -EINVAL;
1533         }
1534
1535         if (queue_id >= rte_sched_port_queues_per_port(port)) {
1536                 RTE_LOG(ERR, SCHED,
1537                         "%s: Incorrect value for queue id\n", __func__);
1538                 return -EINVAL;
1539         }
1540
1541         if (stats == NULL) {
1542                 RTE_LOG(ERR, SCHED,
1543                         "%s: Incorrect value for parameter stats\n", __func__);
1544                 return -EINVAL;
1545         }
1546
1547         if (qlen == NULL) {
1548                 RTE_LOG(ERR, SCHED,
1549                         "%s: Incorrect value for parameter qlen\n", __func__);
1550                 return -EINVAL;
1551         }
1552         q = port->queue + queue_id;
1553         qe = port->queue_extra + queue_id;
1554
1555         /* Copy queue stats and clear */
1556         memcpy(stats, &qe->stats, sizeof(struct rte_sched_queue_stats));
1557         memset(&qe->stats, 0, sizeof(struct rte_sched_queue_stats));
1558
1559         /* Queue length */
1560         *qlen = q->qw - q->qr;
1561
1562         return 0;
1563 }
1564
1565 #ifdef RTE_SCHED_DEBUG
1566
1567 static inline int
1568 rte_sched_port_queue_is_empty(struct rte_sched_port *port, uint32_t qindex)
1569 {
1570         struct rte_sched_queue *queue = port->queue + qindex;
1571
1572         return queue->qr == queue->qw;
1573 }
1574
1575 #endif /* RTE_SCHED_DEBUG */
1576
1577 #ifdef RTE_SCHED_COLLECT_STATS
1578
1579 static inline void
1580 rte_sched_port_update_subport_stats(struct rte_sched_port *port,
1581         struct rte_sched_subport *subport,
1582         uint32_t qindex,
1583         struct rte_mbuf *pkt)
1584 {
1585         uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1586         uint32_t pkt_len = pkt->pkt_len;
1587
1588         subport->stats.n_pkts_tc[tc_index] += 1;
1589         subport->stats.n_bytes_tc[tc_index] += pkt_len;
1590 }
1591
1592 #ifdef RTE_SCHED_RED
1593 static inline void
1594 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1595         struct rte_sched_subport *subport,
1596         uint32_t qindex,
1597         struct rte_mbuf *pkt,
1598         uint32_t red)
1599 #else
1600 static inline void
1601 rte_sched_port_update_subport_stats_on_drop(struct rte_sched_port *port,
1602         struct rte_sched_subport *subport,
1603         uint32_t qindex,
1604         struct rte_mbuf *pkt,
1605         __rte_unused uint32_t red)
1606 #endif
1607 {
1608         uint32_t tc_index = rte_sched_port_pipe_tc(port, qindex);
1609         uint32_t pkt_len = pkt->pkt_len;
1610
1611         subport->stats.n_pkts_tc_dropped[tc_index] += 1;
1612         subport->stats.n_bytes_tc_dropped[tc_index] += pkt_len;
1613 #ifdef RTE_SCHED_RED
1614         subport->stats.n_pkts_red_dropped[tc_index] += red;
1615 #endif
1616 }
1617
1618 static inline void
1619 rte_sched_port_update_queue_stats(struct rte_sched_subport *subport,
1620         uint32_t qindex,
1621         struct rte_mbuf *pkt)
1622 {
1623         struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1624         uint32_t pkt_len = pkt->pkt_len;
1625
1626         qe->stats.n_pkts += 1;
1627         qe->stats.n_bytes += pkt_len;
1628 }
1629
1630 #ifdef RTE_SCHED_RED
1631 static inline void
1632 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1633         uint32_t qindex,
1634         struct rte_mbuf *pkt,
1635         uint32_t red)
1636 #else
1637 static inline void
1638 rte_sched_port_update_queue_stats_on_drop(struct rte_sched_subport *subport,
1639         uint32_t qindex,
1640         struct rte_mbuf *pkt,
1641         __rte_unused uint32_t red)
1642 #endif
1643 {
1644         struct rte_sched_queue_extra *qe = subport->queue_extra + qindex;
1645         uint32_t pkt_len = pkt->pkt_len;
1646
1647         qe->stats.n_pkts_dropped += 1;
1648         qe->stats.n_bytes_dropped += pkt_len;
1649 #ifdef RTE_SCHED_RED
1650         qe->stats.n_pkts_red_dropped += red;
1651 #endif
1652 }
1653
1654 #endif /* RTE_SCHED_COLLECT_STATS */
1655
1656 #ifdef RTE_SCHED_RED
1657
1658 static inline int
1659 rte_sched_port_red_drop(struct rte_sched_port *port,
1660         struct rte_sched_subport *subport,
1661         struct rte_mbuf *pkt,
1662         uint32_t qindex,
1663         uint16_t qlen)
1664 {
1665         struct rte_sched_queue_extra *qe;
1666         struct rte_red_config *red_cfg;
1667         struct rte_red *red;
1668         uint32_t tc_index;
1669         enum rte_color color;
1670
1671         tc_index = rte_sched_port_pipe_tc(port, qindex);
1672         color = rte_sched_port_pkt_read_color(pkt);
1673         red_cfg = &subport->red_config[tc_index][color];
1674
1675         if ((red_cfg->min_th | red_cfg->max_th) == 0)
1676                 return 0;
1677
1678         qe = subport->queue_extra + qindex;
1679         red = &qe->red;
1680
1681         return rte_red_enqueue(red_cfg, red, qlen, port->time);
1682 }
1683
1684 static inline void
1685 rte_sched_port_set_queue_empty_timestamp(struct rte_sched_port *port, uint32_t qindex)
1686 {
1687         struct rte_sched_queue_extra *qe = port->queue_extra + qindex;
1688         struct rte_red *red = &qe->red;
1689
1690         rte_red_mark_queue_empty(red, port->time);
1691 }
1692
1693 #else
1694
1695 static inline int rte_sched_port_red_drop(struct rte_sched_port *port __rte_unused,
1696         struct rte_sched_subport *subport __rte_unused,
1697         struct rte_mbuf *pkt __rte_unused,
1698         uint32_t qindex __rte_unused,
1699         uint16_t qlen __rte_unused)
1700 {
1701         return 0;
1702 }
1703
1704 #define rte_sched_port_set_queue_empty_timestamp(port, qindex)
1705
1706 #endif /* RTE_SCHED_RED */
1707
1708 #ifdef RTE_SCHED_DEBUG
1709
1710 static inline void
1711 debug_check_queue_slab(struct rte_sched_port *port, uint32_t bmp_pos,
1712                        uint64_t bmp_slab)
1713 {
1714         uint64_t mask;
1715         uint32_t i, panic;
1716
1717         if (bmp_slab == 0)
1718                 rte_panic("Empty slab at position %u\n", bmp_pos);
1719
1720         panic = 0;
1721         for (i = 0, mask = 1; i < 64; i++, mask <<= 1) {
1722                 if (mask & bmp_slab) {
1723                         if (rte_sched_port_queue_is_empty(port, bmp_pos + i)) {
1724                                 printf("Queue %u (slab offset %u) is empty\n", bmp_pos + i, i);
1725                                 panic = 1;
1726                         }
1727                 }
1728         }
1729
1730         if (panic)
1731                 rte_panic("Empty queues in slab 0x%" PRIx64 "starting at position %u\n",
1732                         bmp_slab, bmp_pos);
1733 }
1734
1735 #endif /* RTE_SCHED_DEBUG */
1736
1737 static inline struct rte_sched_subport *
1738 rte_sched_port_subport(struct rte_sched_port *port,
1739         struct rte_mbuf *pkt)
1740 {
1741         uint32_t queue_id = rte_mbuf_sched_queue_get(pkt);
1742         uint32_t subport_id = queue_id >> (port->n_pipes_per_subport_log2 + 4);
1743
1744         return port->subports[subport_id];
1745 }
1746
1747 static inline uint32_t
1748 rte_sched_port_enqueue_qptrs_prefetch0(struct rte_sched_subport *subport,
1749         struct rte_mbuf *pkt, uint32_t subport_qmask)
1750 {
1751         struct rte_sched_queue *q;
1752 #ifdef RTE_SCHED_COLLECT_STATS
1753         struct rte_sched_queue_extra *qe;
1754 #endif
1755         uint32_t qindex = rte_mbuf_sched_queue_get(pkt);
1756         uint32_t subport_queue_id = subport_qmask & qindex;
1757
1758         q = subport->queue + subport_queue_id;
1759         rte_prefetch0(q);
1760 #ifdef RTE_SCHED_COLLECT_STATS
1761         qe = subport->queue_extra + subport_queue_id;
1762         rte_prefetch0(qe);
1763 #endif
1764
1765         return subport_queue_id;
1766 }
1767
1768 static inline void
1769 rte_sched_port_enqueue_qwa_prefetch0(struct rte_sched_port *port,
1770         struct rte_sched_subport *subport,
1771         uint32_t qindex,
1772         struct rte_mbuf **qbase)
1773 {
1774         struct rte_sched_queue *q;
1775         struct rte_mbuf **q_qw;
1776         uint16_t qsize;
1777
1778         q = subport->queue + qindex;
1779         qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1780         q_qw = qbase + (q->qw & (qsize - 1));
1781
1782         rte_prefetch0(q_qw);
1783         rte_bitmap_prefetch0(subport->bmp, qindex);
1784 }
1785
1786 static inline int
1787 rte_sched_port_enqueue_qwa(struct rte_sched_port *port,
1788         struct rte_sched_subport *subport,
1789         uint32_t qindex,
1790         struct rte_mbuf **qbase,
1791         struct rte_mbuf *pkt)
1792 {
1793         struct rte_sched_queue *q;
1794         uint16_t qsize;
1795         uint16_t qlen;
1796
1797         q = subport->queue + qindex;
1798         qsize = rte_sched_subport_pipe_qsize(port, subport, qindex);
1799         qlen = q->qw - q->qr;
1800
1801         /* Drop the packet (and update drop stats) when queue is full */
1802         if (unlikely(rte_sched_port_red_drop(port, subport, pkt, qindex, qlen) ||
1803                      (qlen >= qsize))) {
1804                 rte_pktmbuf_free(pkt);
1805 #ifdef RTE_SCHED_COLLECT_STATS
1806                 rte_sched_port_update_subport_stats_on_drop(port, subport,
1807                         qindex, pkt, qlen < qsize);
1808                 rte_sched_port_update_queue_stats_on_drop(subport, qindex, pkt,
1809                         qlen < qsize);
1810 #endif
1811                 return 0;
1812         }
1813
1814         /* Enqueue packet */
1815         qbase[q->qw & (qsize - 1)] = pkt;
1816         q->qw++;
1817
1818         /* Activate queue in the subport bitmap */
1819         rte_bitmap_set(subport->bmp, qindex);
1820
1821         /* Statistics */
1822 #ifdef RTE_SCHED_COLLECT_STATS
1823         rte_sched_port_update_subport_stats(port, subport, qindex, pkt);
1824         rte_sched_port_update_queue_stats(subport, qindex, pkt);
1825 #endif
1826
1827         return 1;
1828 }
1829
1830
1831 /*
1832  * The enqueue function implements a 4-level pipeline with each stage
1833  * processing two different packets. The purpose of using a pipeline
1834  * is to hide the latency of prefetching the data structures. The
1835  * naming convention is presented in the diagram below:
1836  *
1837  *   p00  _______   p10  _______   p20  _______   p30  _______
1838  * ----->|       |----->|       |----->|       |----->|       |----->
1839  *       |   0   |      |   1   |      |   2   |      |   3   |
1840  * ----->|_______|----->|_______|----->|_______|----->|_______|----->
1841  *   p01            p11            p21            p31
1842  *
1843  */
1844 int
1845 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts,
1846                        uint32_t n_pkts)
1847 {
1848         struct rte_mbuf *pkt00, *pkt01, *pkt10, *pkt11, *pkt20, *pkt21,
1849                 *pkt30, *pkt31, *pkt_last;
1850         struct rte_mbuf **q00_base, **q01_base, **q10_base, **q11_base,
1851                 **q20_base, **q21_base, **q30_base, **q31_base, **q_last_base;
1852         struct rte_sched_subport *subport00, *subport01, *subport10, *subport11,
1853                 *subport20, *subport21, *subport30, *subport31, *subport_last;
1854         uint32_t q00, q01, q10, q11, q20, q21, q30, q31, q_last;
1855         uint32_t r00, r01, r10, r11, r20, r21, r30, r31, r_last;
1856         uint32_t subport_qmask;
1857         uint32_t result, i;
1858
1859         result = 0;
1860         subport_qmask = (1 << (port->n_pipes_per_subport_log2 + 4)) - 1;
1861
1862         /*
1863          * Less then 6 input packets available, which is not enough to
1864          * feed the pipeline
1865          */
1866         if (unlikely(n_pkts < 6)) {
1867                 struct rte_sched_subport *subports[5];
1868                 struct rte_mbuf **q_base[5];
1869                 uint32_t q[5];
1870
1871                 /* Prefetch the mbuf structure of each packet */
1872                 for (i = 0; i < n_pkts; i++)
1873                         rte_prefetch0(pkts[i]);
1874
1875                 /* Prefetch the subport structure for each packet */
1876                 for (i = 0; i < n_pkts; i++)
1877                         subports[i] = rte_sched_port_subport(port, pkts[i]);
1878
1879                 /* Prefetch the queue structure for each queue */
1880                 for (i = 0; i < n_pkts; i++)
1881                         q[i] = rte_sched_port_enqueue_qptrs_prefetch0(subports[i],
1882                                         pkts[i], subport_qmask);
1883
1884                 /* Prefetch the write pointer location of each queue */
1885                 for (i = 0; i < n_pkts; i++) {
1886                         q_base[i] = rte_sched_subport_pipe_qbase(subports[i], q[i]);
1887                         rte_sched_port_enqueue_qwa_prefetch0(port, subports[i],
1888                                 q[i], q_base[i]);
1889                 }
1890
1891                 /* Write each packet to its queue */
1892                 for (i = 0; i < n_pkts; i++)
1893                         result += rte_sched_port_enqueue_qwa(port, subports[i],
1894                                                 q[i], q_base[i], pkts[i]);
1895
1896                 return result;
1897         }
1898
1899         /* Feed the first 3 stages of the pipeline (6 packets needed) */
1900         pkt20 = pkts[0];
1901         pkt21 = pkts[1];
1902         rte_prefetch0(pkt20);
1903         rte_prefetch0(pkt21);
1904
1905         pkt10 = pkts[2];
1906         pkt11 = pkts[3];
1907         rte_prefetch0(pkt10);
1908         rte_prefetch0(pkt11);
1909
1910         subport20 = rte_sched_port_subport(port, pkt20);
1911         subport21 = rte_sched_port_subport(port, pkt21);
1912         q20 = rte_sched_port_enqueue_qptrs_prefetch0(subport20,
1913                         pkt20, subport_qmask);
1914         q21 = rte_sched_port_enqueue_qptrs_prefetch0(subport21,
1915                         pkt21, subport_qmask);
1916
1917         pkt00 = pkts[4];
1918         pkt01 = pkts[5];
1919         rte_prefetch0(pkt00);
1920         rte_prefetch0(pkt01);
1921
1922         subport10 = rte_sched_port_subport(port, pkt10);
1923         subport11 = rte_sched_port_subport(port, pkt11);
1924         q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
1925                         pkt10, subport_qmask);
1926         q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
1927                         pkt11, subport_qmask);
1928
1929         q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
1930         q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
1931         rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
1932         rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
1933
1934         /* Run the pipeline */
1935         for (i = 6; i < (n_pkts & (~1)); i += 2) {
1936                 /* Propagate stage inputs */
1937                 pkt30 = pkt20;
1938                 pkt31 = pkt21;
1939                 pkt20 = pkt10;
1940                 pkt21 = pkt11;
1941                 pkt10 = pkt00;
1942                 pkt11 = pkt01;
1943                 q30 = q20;
1944                 q31 = q21;
1945                 q20 = q10;
1946                 q21 = q11;
1947                 subport30 = subport20;
1948                 subport31 = subport21;
1949                 subport20 = subport10;
1950                 subport21 = subport11;
1951                 q30_base = q20_base;
1952                 q31_base = q21_base;
1953
1954                 /* Stage 0: Get packets in */
1955                 pkt00 = pkts[i];
1956                 pkt01 = pkts[i + 1];
1957                 rte_prefetch0(pkt00);
1958                 rte_prefetch0(pkt01);
1959
1960                 /* Stage 1: Prefetch subport and queue structure storing queue pointers */
1961                 subport10 = rte_sched_port_subport(port, pkt10);
1962                 subport11 = rte_sched_port_subport(port, pkt11);
1963                 q10 = rte_sched_port_enqueue_qptrs_prefetch0(subport10,
1964                                 pkt10, subport_qmask);
1965                 q11 = rte_sched_port_enqueue_qptrs_prefetch0(subport11,
1966                                 pkt11, subport_qmask);
1967
1968                 /* Stage 2: Prefetch queue write location */
1969                 q20_base = rte_sched_subport_pipe_qbase(subport20, q20);
1970                 q21_base = rte_sched_subport_pipe_qbase(subport21, q21);
1971                 rte_sched_port_enqueue_qwa_prefetch0(port, subport20, q20, q20_base);
1972                 rte_sched_port_enqueue_qwa_prefetch0(port, subport21, q21, q21_base);
1973
1974                 /* Stage 3: Write packet to queue and activate queue */
1975                 r30 = rte_sched_port_enqueue_qwa(port, subport30,
1976                                 q30, q30_base, pkt30);
1977                 r31 = rte_sched_port_enqueue_qwa(port, subport31,
1978                                 q31, q31_base, pkt31);
1979                 result += r30 + r31;
1980         }
1981
1982         /*
1983          * Drain the pipeline (exactly 6 packets).
1984          * Handle the last packet in the case
1985          * of an odd number of input packets.
1986          */
1987         pkt_last = pkts[n_pkts - 1];
1988         rte_prefetch0(pkt_last);
1989
1990         subport00 = rte_sched_port_subport(port, pkt00);
1991         subport01 = rte_sched_port_subport(port, pkt01);
1992         q00 = rte_sched_port_enqueue_qptrs_prefetch0(subport00,
1993                         pkt00, subport_qmask);
1994         q01 = rte_sched_port_enqueue_qptrs_prefetch0(subport01,
1995                         pkt01, subport_qmask);
1996
1997         q10_base = rte_sched_subport_pipe_qbase(subport10, q10);
1998         q11_base = rte_sched_subport_pipe_qbase(subport11, q11);
1999         rte_sched_port_enqueue_qwa_prefetch0(port, subport10, q10, q10_base);
2000         rte_sched_port_enqueue_qwa_prefetch0(port, subport11, q11, q11_base);
2001
2002         r20 = rte_sched_port_enqueue_qwa(port, subport20,
2003                         q20, q20_base, pkt20);
2004         r21 = rte_sched_port_enqueue_qwa(port, subport21,
2005                         q21, q21_base, pkt21);
2006         result += r20 + r21;
2007
2008         subport_last = rte_sched_port_subport(port, pkt_last);
2009         q_last = rte_sched_port_enqueue_qptrs_prefetch0(subport_last,
2010                                 pkt_last, subport_qmask);
2011
2012         q00_base = rte_sched_subport_pipe_qbase(subport00, q00);
2013         q01_base = rte_sched_subport_pipe_qbase(subport01, q01);
2014         rte_sched_port_enqueue_qwa_prefetch0(port, subport00, q00, q00_base);
2015         rte_sched_port_enqueue_qwa_prefetch0(port, subport01, q01, q01_base);
2016
2017         r10 = rte_sched_port_enqueue_qwa(port, subport10, q10,
2018                         q10_base, pkt10);
2019         r11 = rte_sched_port_enqueue_qwa(port, subport11, q11,
2020                         q11_base, pkt11);
2021         result += r10 + r11;
2022
2023         q_last_base = rte_sched_subport_pipe_qbase(subport_last, q_last);
2024         rte_sched_port_enqueue_qwa_prefetch0(port, subport_last,
2025                 q_last, q_last_base);
2026
2027         r00 = rte_sched_port_enqueue_qwa(port, subport00, q00,
2028                         q00_base, pkt00);
2029         r01 = rte_sched_port_enqueue_qwa(port, subport01, q01,
2030                         q01_base, pkt01);
2031         result += r00 + r01;
2032
2033         if (n_pkts & 1) {
2034                 r_last = rte_sched_port_enqueue_qwa(port, subport_last,
2035                                         q_last, q_last_base, pkt_last);
2036                 result += r_last;
2037         }
2038
2039         return result;
2040 }
2041
2042 #ifndef RTE_SCHED_SUBPORT_TC_OV
2043
2044 static inline void
2045 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
2046 {
2047         struct rte_sched_grinder *grinder = port->grinder + pos;
2048         struct rte_sched_subport *subport = grinder->subport;
2049         struct rte_sched_pipe *pipe = grinder->pipe;
2050         struct rte_sched_pipe_profile *params = grinder->pipe_params;
2051         uint64_t n_periods;
2052         uint32_t i;
2053
2054         /* Subport TB */
2055         n_periods = (port->time - subport->tb_time) / subport->tb_period;
2056         subport->tb_credits += n_periods * subport->tb_credits_per_period;
2057         subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
2058         subport->tb_time += n_periods * subport->tb_period;
2059
2060         /* Pipe TB */
2061         n_periods = (port->time - pipe->tb_time) / params->tb_period;
2062         pipe->tb_credits += n_periods * params->tb_credits_per_period;
2063         pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
2064         pipe->tb_time += n_periods * params->tb_period;
2065
2066         /* Subport TCs */
2067         if (unlikely(port->time >= subport->tc_time)) {
2068                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2069                         subport->tc_credits[i] = subport->tc_credits_per_period[i];
2070
2071                 subport->tc_time = port->time + subport->tc_period;
2072         }
2073
2074         /* Pipe TCs */
2075         if (unlikely(port->time >= pipe->tc_time)) {
2076                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2077                         pipe->tc_credits[i] = params->tc_credits_per_period[i];
2078
2079                 pipe->tc_time = port->time + params->tc_period;
2080         }
2081 }
2082
2083 #else
2084
2085 static inline uint32_t
2086 grinder_tc_ov_credits_update(struct rte_sched_port *port, uint32_t pos)
2087 {
2088         struct rte_sched_grinder *grinder = port->grinder + pos;
2089         struct rte_sched_subport *subport = grinder->subport;
2090         uint32_t tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2091         uint32_t tc_consumption = 0, tc_ov_consumption_max;
2092         uint32_t tc_ov_wm = subport->tc_ov_wm;
2093         uint32_t i;
2094
2095         if (subport->tc_ov == 0)
2096                 return subport->tc_ov_wm_max;
2097
2098         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2099                 tc_ov_consumption[i] =
2100                         subport->tc_credits_per_period[i] - subport->tc_credits[i];
2101                 tc_consumption += tc_ov_consumption[i];
2102         }
2103
2104         tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] =
2105                 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2106                 subport->tc_credits[RTE_SCHED_TRAFFIC_CLASS_BE];
2107
2108         tc_ov_consumption_max =
2109                 subport->tc_credits_per_period[RTE_SCHED_TRAFFIC_CLASS_BE] -
2110                         tc_consumption;
2111
2112         if (tc_ov_consumption[RTE_SCHED_TRAFFIC_CLASS_BE] >
2113                 (tc_ov_consumption_max - port->mtu)) {
2114                 tc_ov_wm  -= tc_ov_wm >> 7;
2115                 if (tc_ov_wm < subport->tc_ov_wm_min)
2116                         tc_ov_wm = subport->tc_ov_wm_min;
2117
2118                 return tc_ov_wm;
2119         }
2120
2121         tc_ov_wm += (tc_ov_wm >> 7) + 1;
2122         if (tc_ov_wm > subport->tc_ov_wm_max)
2123                 tc_ov_wm = subport->tc_ov_wm_max;
2124
2125         return tc_ov_wm;
2126 }
2127
2128 static inline void
2129 grinder_credits_update(struct rte_sched_port *port, uint32_t pos)
2130 {
2131         struct rte_sched_grinder *grinder = port->grinder + pos;
2132         struct rte_sched_subport *subport = grinder->subport;
2133         struct rte_sched_pipe *pipe = grinder->pipe;
2134         struct rte_sched_pipe_profile *params = grinder->pipe_params;
2135         uint64_t n_periods;
2136         uint32_t i;
2137
2138         /* Subport TB */
2139         n_periods = (port->time - subport->tb_time) / subport->tb_period;
2140         subport->tb_credits += n_periods * subport->tb_credits_per_period;
2141         subport->tb_credits = rte_sched_min_val_2_u32(subport->tb_credits, subport->tb_size);
2142         subport->tb_time += n_periods * subport->tb_period;
2143
2144         /* Pipe TB */
2145         n_periods = (port->time - pipe->tb_time) / params->tb_period;
2146         pipe->tb_credits += n_periods * params->tb_credits_per_period;
2147         pipe->tb_credits = rte_sched_min_val_2_u32(pipe->tb_credits, params->tb_size);
2148         pipe->tb_time += n_periods * params->tb_period;
2149
2150         /* Subport TCs */
2151         if (unlikely(port->time >= subport->tc_time)) {
2152                 subport->tc_ov_wm = grinder_tc_ov_credits_update(port, pos);
2153
2154                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2155                         subport->tc_credits[i] = subport->tc_credits_per_period[i];
2156
2157                 subport->tc_time = port->time + subport->tc_period;
2158                 subport->tc_ov_period_id++;
2159         }
2160
2161         /* Pipe TCs */
2162         if (unlikely(port->time >= pipe->tc_time)) {
2163                 for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2164                         pipe->tc_credits[i] = params->tc_credits_per_period[i];
2165                 pipe->tc_time = port->time + params->tc_period;
2166         }
2167
2168         /* Pipe TCs - Oversubscription */
2169         if (unlikely(pipe->tc_ov_period_id != subport->tc_ov_period_id)) {
2170                 pipe->tc_ov_credits = subport->tc_ov_wm * params->tc_ov_weight;
2171
2172                 pipe->tc_ov_period_id = subport->tc_ov_period_id;
2173         }
2174 }
2175
2176 #endif /* RTE_SCHED_TS_CREDITS_UPDATE, RTE_SCHED_SUBPORT_TC_OV */
2177
2178
2179 #ifndef RTE_SCHED_SUBPORT_TC_OV
2180
2181 static inline int
2182 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
2183 {
2184         struct rte_sched_grinder *grinder = port->grinder + pos;
2185         struct rte_sched_subport *subport = grinder->subport;
2186         struct rte_sched_pipe *pipe = grinder->pipe;
2187         struct rte_mbuf *pkt = grinder->pkt;
2188         uint32_t tc_index = grinder->tc_index;
2189         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2190         uint32_t subport_tb_credits = subport->tb_credits;
2191         uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2192         uint32_t pipe_tb_credits = pipe->tb_credits;
2193         uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2194         int enough_credits;
2195
2196         /* Check queue credits */
2197         enough_credits = (pkt_len <= subport_tb_credits) &&
2198                 (pkt_len <= subport_tc_credits) &&
2199                 (pkt_len <= pipe_tb_credits) &&
2200                 (pkt_len <= pipe_tc_credits);
2201
2202         if (!enough_credits)
2203                 return 0;
2204
2205         /* Update port credits */
2206         subport->tb_credits -= pkt_len;
2207         subport->tc_credits[tc_index] -= pkt_len;
2208         pipe->tb_credits -= pkt_len;
2209         pipe->tc_credits[tc_index] -= pkt_len;
2210
2211         return 1;
2212 }
2213
2214 #else
2215
2216 static inline int
2217 grinder_credits_check(struct rte_sched_port *port, uint32_t pos)
2218 {
2219         struct rte_sched_grinder *grinder = port->grinder + pos;
2220         struct rte_sched_subport *subport = grinder->subport;
2221         struct rte_sched_pipe *pipe = grinder->pipe;
2222         struct rte_mbuf *pkt = grinder->pkt;
2223         uint32_t tc_index = grinder->tc_index;
2224         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2225         uint32_t subport_tb_credits = subport->tb_credits;
2226         uint32_t subport_tc_credits = subport->tc_credits[tc_index];
2227         uint32_t pipe_tb_credits = pipe->tb_credits;
2228         uint32_t pipe_tc_credits = pipe->tc_credits[tc_index];
2229         uint32_t pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
2230         uint32_t pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE] = {0};
2231         uint32_t pipe_tc_ov_credits, i;
2232         int enough_credits;
2233
2234         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE; i++)
2235                 pipe_tc_ov_mask1[i] = UINT32_MAX;
2236
2237         pipe_tc_ov_mask1[RTE_SCHED_TRAFFIC_CLASS_BE] = pipe->tc_ov_credits;
2238         pipe_tc_ov_mask2[RTE_SCHED_TRAFFIC_CLASS_BE] = UINT32_MAX;
2239         pipe_tc_ov_credits = pipe_tc_ov_mask1[tc_index];
2240
2241         /* Check pipe and subport credits */
2242         enough_credits = (pkt_len <= subport_tb_credits) &&
2243                 (pkt_len <= subport_tc_credits) &&
2244                 (pkt_len <= pipe_tb_credits) &&
2245                 (pkt_len <= pipe_tc_credits) &&
2246                 (pkt_len <= pipe_tc_ov_credits);
2247
2248         if (!enough_credits)
2249                 return 0;
2250
2251         /* Update pipe and subport credits */
2252         subport->tb_credits -= pkt_len;
2253         subport->tc_credits[tc_index] -= pkt_len;
2254         pipe->tb_credits -= pkt_len;
2255         pipe->tc_credits[tc_index] -= pkt_len;
2256         pipe->tc_ov_credits -= pipe_tc_ov_mask2[tc_index] & pkt_len;
2257
2258         return 1;
2259 }
2260
2261 #endif /* RTE_SCHED_SUBPORT_TC_OV */
2262
2263
2264 static inline int
2265 grinder_schedule(struct rte_sched_port *port, uint32_t pos)
2266 {
2267         struct rte_sched_grinder *grinder = port->grinder + pos;
2268         struct rte_sched_queue *queue = grinder->queue[grinder->qpos];
2269         struct rte_mbuf *pkt = grinder->pkt;
2270         uint32_t pkt_len = pkt->pkt_len + port->frame_overhead;
2271         uint32_t be_tc_active;
2272
2273         if (!grinder_credits_check(port, pos))
2274                 return 0;
2275
2276         /* Advance port time */
2277         port->time += pkt_len;
2278
2279         /* Send packet */
2280         port->pkts_out[port->n_pkts_out++] = pkt;
2281         queue->qr++;
2282
2283         be_tc_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE) ? ~0x0 : 0x0;
2284         grinder->wrr_tokens[grinder->qpos] +=
2285                 (pkt_len * grinder->wrr_cost[grinder->qpos]) & be_tc_active;
2286
2287         if (queue->qr == queue->qw) {
2288                 uint32_t qindex = grinder->qindex[grinder->qpos];
2289
2290                 rte_bitmap_clear(port->bmp, qindex);
2291                 grinder->qmask &= ~(1 << grinder->qpos);
2292                 if (be_tc_active)
2293                         grinder->wrr_mask[grinder->qpos] = 0;
2294                 rte_sched_port_set_queue_empty_timestamp(port, qindex);
2295         }
2296
2297         /* Reset pipe loop detection */
2298         port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2299         grinder->productive = 1;
2300
2301         return 1;
2302 }
2303
2304 #ifdef SCHED_VECTOR_SSE4
2305
2306 static inline int
2307 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2308 {
2309         __m128i index = _mm_set1_epi32(base_pipe);
2310         __m128i pipes = _mm_load_si128((__m128i *)port->grinder_base_bmp_pos);
2311         __m128i res = _mm_cmpeq_epi32(pipes, index);
2312
2313         pipes = _mm_load_si128((__m128i *)(port->grinder_base_bmp_pos + 4));
2314         pipes = _mm_cmpeq_epi32(pipes, index);
2315         res = _mm_or_si128(res, pipes);
2316
2317         if (_mm_testz_si128(res, res))
2318                 return 0;
2319
2320         return 1;
2321 }
2322
2323 #elif defined(SCHED_VECTOR_NEON)
2324
2325 static inline int
2326 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2327 {
2328         uint32x4_t index, pipes;
2329         uint32_t *pos = (uint32_t *)port->grinder_base_bmp_pos;
2330
2331         index = vmovq_n_u32(base_pipe);
2332         pipes = vld1q_u32(pos);
2333         if (!vminvq_u32(veorq_u32(pipes, index)))
2334                 return 1;
2335
2336         pipes = vld1q_u32(pos + 4);
2337         if (!vminvq_u32(veorq_u32(pipes, index)))
2338                 return 1;
2339
2340         return 0;
2341 }
2342
2343 #else
2344
2345 static inline int
2346 grinder_pipe_exists(struct rte_sched_port *port, uint32_t base_pipe)
2347 {
2348         uint32_t i;
2349
2350         for (i = 0; i < RTE_SCHED_PORT_N_GRINDERS; i++) {
2351                 if (port->grinder_base_bmp_pos[i] == base_pipe)
2352                         return 1;
2353         }
2354
2355         return 0;
2356 }
2357
2358 #endif /* RTE_SCHED_OPTIMIZATIONS */
2359
2360 static inline void
2361 grinder_pcache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t bmp_pos, uint64_t bmp_slab)
2362 {
2363         struct rte_sched_grinder *grinder = port->grinder + pos;
2364         uint16_t w[4];
2365
2366         grinder->pcache_w = 0;
2367         grinder->pcache_r = 0;
2368
2369         w[0] = (uint16_t) bmp_slab;
2370         w[1] = (uint16_t) (bmp_slab >> 16);
2371         w[2] = (uint16_t) (bmp_slab >> 32);
2372         w[3] = (uint16_t) (bmp_slab >> 48);
2373
2374         grinder->pcache_qmask[grinder->pcache_w] = w[0];
2375         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos;
2376         grinder->pcache_w += (w[0] != 0);
2377
2378         grinder->pcache_qmask[grinder->pcache_w] = w[1];
2379         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 16;
2380         grinder->pcache_w += (w[1] != 0);
2381
2382         grinder->pcache_qmask[grinder->pcache_w] = w[2];
2383         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 32;
2384         grinder->pcache_w += (w[2] != 0);
2385
2386         grinder->pcache_qmask[grinder->pcache_w] = w[3];
2387         grinder->pcache_qindex[grinder->pcache_w] = bmp_pos + 48;
2388         grinder->pcache_w += (w[3] != 0);
2389 }
2390
2391 static inline void
2392 grinder_tccache_populate(struct rte_sched_port *port, uint32_t pos, uint32_t qindex, uint16_t qmask)
2393 {
2394         struct rte_sched_grinder *grinder = port->grinder + pos;
2395         uint8_t b, i;
2396
2397         grinder->tccache_w = 0;
2398         grinder->tccache_r = 0;
2399
2400         for (i = 0; i < RTE_SCHED_TRAFFIC_CLASS_BE; i++) {
2401                 b = (uint8_t) ((qmask >> i) & 0x1);
2402                 grinder->tccache_qmask[grinder->tccache_w] = b;
2403                 grinder->tccache_qindex[grinder->tccache_w] = qindex + i;
2404                 grinder->tccache_w += (b != 0);
2405         }
2406
2407         b = (uint8_t) (qmask >> (RTE_SCHED_TRAFFIC_CLASS_BE));
2408         grinder->tccache_qmask[grinder->tccache_w] = b;
2409         grinder->tccache_qindex[grinder->tccache_w] = qindex +
2410                 RTE_SCHED_TRAFFIC_CLASS_BE;
2411         grinder->tccache_w += (b != 0);
2412 }
2413
2414 static inline int
2415 grinder_next_tc(struct rte_sched_port *port, uint32_t pos)
2416 {
2417         struct rte_sched_grinder *grinder = port->grinder + pos;
2418         struct rte_mbuf **qbase;
2419         uint32_t qindex;
2420         uint16_t qsize;
2421
2422         if (grinder->tccache_r == grinder->tccache_w)
2423                 return 0;
2424
2425         qindex = grinder->tccache_qindex[grinder->tccache_r];
2426         qbase = rte_sched_port_qbase(port, qindex);
2427         qsize = rte_sched_port_qsize(port, qindex);
2428
2429         grinder->tc_index = rte_sched_port_pipe_tc(port, qindex);
2430         grinder->qmask = grinder->tccache_qmask[grinder->tccache_r];
2431         grinder->qsize = qsize;
2432
2433         if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2434                 grinder->queue[0] = port->queue + qindex;
2435                 grinder->qbase[0] = qbase;
2436                 grinder->qindex[0] = qindex;
2437                 grinder->tccache_r++;
2438
2439                 return 1;
2440         }
2441
2442         grinder->queue[0] = port->queue + qindex;
2443         grinder->queue[1] = port->queue + qindex + 1;
2444         grinder->queue[2] = port->queue + qindex + 2;
2445         grinder->queue[3] = port->queue + qindex + 3;
2446
2447         grinder->qbase[0] = qbase;
2448         grinder->qbase[1] = qbase + qsize;
2449         grinder->qbase[2] = qbase + 2 * qsize;
2450         grinder->qbase[3] = qbase + 3 * qsize;
2451
2452         grinder->qindex[0] = qindex;
2453         grinder->qindex[1] = qindex + 1;
2454         grinder->qindex[2] = qindex + 2;
2455         grinder->qindex[3] = qindex + 3;
2456
2457         grinder->tccache_r++;
2458         return 1;
2459 }
2460
2461 static inline int
2462 grinder_next_pipe(struct rte_sched_port *port, uint32_t pos)
2463 {
2464         struct rte_sched_grinder *grinder = port->grinder + pos;
2465         uint32_t pipe_qindex;
2466         uint16_t pipe_qmask;
2467
2468         if (grinder->pcache_r < grinder->pcache_w) {
2469                 pipe_qmask = grinder->pcache_qmask[grinder->pcache_r];
2470                 pipe_qindex = grinder->pcache_qindex[grinder->pcache_r];
2471                 grinder->pcache_r++;
2472         } else {
2473                 uint64_t bmp_slab = 0;
2474                 uint32_t bmp_pos = 0;
2475
2476                 /* Get another non-empty pipe group */
2477                 if (unlikely(rte_bitmap_scan(port->bmp, &bmp_pos, &bmp_slab) <= 0))
2478                         return 0;
2479
2480 #ifdef RTE_SCHED_DEBUG
2481                 debug_check_queue_slab(port, bmp_pos, bmp_slab);
2482 #endif
2483
2484                 /* Return if pipe group already in one of the other grinders */
2485                 port->grinder_base_bmp_pos[pos] = RTE_SCHED_BMP_POS_INVALID;
2486                 if (unlikely(grinder_pipe_exists(port, bmp_pos)))
2487                         return 0;
2488
2489                 port->grinder_base_bmp_pos[pos] = bmp_pos;
2490
2491                 /* Install new pipe group into grinder's pipe cache */
2492                 grinder_pcache_populate(port, pos, bmp_pos, bmp_slab);
2493
2494                 pipe_qmask = grinder->pcache_qmask[0];
2495                 pipe_qindex = grinder->pcache_qindex[0];
2496                 grinder->pcache_r = 1;
2497         }
2498
2499         /* Install new pipe in the grinder */
2500         grinder->pindex = pipe_qindex >> 4;
2501         grinder->subport = port->subport + (grinder->pindex / port->n_pipes_per_subport);
2502         grinder->pipe = port->pipe + grinder->pindex;
2503         grinder->pipe_params = NULL; /* to be set after the pipe structure is prefetched */
2504         grinder->productive = 0;
2505
2506         grinder_tccache_populate(port, pos, pipe_qindex, pipe_qmask);
2507         grinder_next_tc(port, pos);
2508
2509         /* Check for pipe exhaustion */
2510         if (grinder->pindex == port->pipe_loop) {
2511                 port->pipe_exhaustion = 1;
2512                 port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2513         }
2514
2515         return 1;
2516 }
2517
2518
2519 static inline void
2520 grinder_wrr_load(struct rte_sched_port *port, uint32_t pos)
2521 {
2522         struct rte_sched_grinder *grinder = port->grinder + pos;
2523         struct rte_sched_pipe *pipe = grinder->pipe;
2524         struct rte_sched_pipe_profile *pipe_params = grinder->pipe_params;
2525         uint32_t qmask = grinder->qmask;
2526
2527         grinder->wrr_tokens[0] =
2528                 ((uint16_t) pipe->wrr_tokens[0]) << RTE_SCHED_WRR_SHIFT;
2529         grinder->wrr_tokens[1] =
2530                 ((uint16_t) pipe->wrr_tokens[1]) << RTE_SCHED_WRR_SHIFT;
2531         grinder->wrr_tokens[2] =
2532                 ((uint16_t) pipe->wrr_tokens[2]) << RTE_SCHED_WRR_SHIFT;
2533         grinder->wrr_tokens[3] =
2534                 ((uint16_t) pipe->wrr_tokens[3]) << RTE_SCHED_WRR_SHIFT;
2535
2536         grinder->wrr_mask[0] = (qmask & 0x1) * 0xFFFF;
2537         grinder->wrr_mask[1] = ((qmask >> 1) & 0x1) * 0xFFFF;
2538         grinder->wrr_mask[2] = ((qmask >> 2) & 0x1) * 0xFFFF;
2539         grinder->wrr_mask[3] = ((qmask >> 3) & 0x1) * 0xFFFF;
2540
2541         grinder->wrr_cost[0] = pipe_params->wrr_cost[0];
2542         grinder->wrr_cost[1] = pipe_params->wrr_cost[1];
2543         grinder->wrr_cost[2] = pipe_params->wrr_cost[2];
2544         grinder->wrr_cost[3] = pipe_params->wrr_cost[3];
2545 }
2546
2547 static inline void
2548 grinder_wrr_store(struct rte_sched_port *port, uint32_t pos)
2549 {
2550         struct rte_sched_grinder *grinder = port->grinder + pos;
2551         struct rte_sched_pipe *pipe = grinder->pipe;
2552
2553         pipe->wrr_tokens[0] =
2554                         (grinder->wrr_tokens[0] & grinder->wrr_mask[0]) >>
2555                                 RTE_SCHED_WRR_SHIFT;
2556         pipe->wrr_tokens[1] =
2557                         (grinder->wrr_tokens[1] & grinder->wrr_mask[1]) >>
2558                                 RTE_SCHED_WRR_SHIFT;
2559         pipe->wrr_tokens[2] =
2560                         (grinder->wrr_tokens[2] & grinder->wrr_mask[2]) >>
2561                                 RTE_SCHED_WRR_SHIFT;
2562         pipe->wrr_tokens[3] =
2563                         (grinder->wrr_tokens[3] & grinder->wrr_mask[3]) >>
2564                                 RTE_SCHED_WRR_SHIFT;
2565 }
2566
2567 static inline void
2568 grinder_wrr(struct rte_sched_port *port, uint32_t pos)
2569 {
2570         struct rte_sched_grinder *grinder = port->grinder + pos;
2571         uint16_t wrr_tokens_min;
2572
2573         grinder->wrr_tokens[0] |= ~grinder->wrr_mask[0];
2574         grinder->wrr_tokens[1] |= ~grinder->wrr_mask[1];
2575         grinder->wrr_tokens[2] |= ~grinder->wrr_mask[2];
2576         grinder->wrr_tokens[3] |= ~grinder->wrr_mask[3];
2577
2578         grinder->qpos = rte_min_pos_4_u16(grinder->wrr_tokens);
2579         wrr_tokens_min = grinder->wrr_tokens[grinder->qpos];
2580
2581         grinder->wrr_tokens[0] -= wrr_tokens_min;
2582         grinder->wrr_tokens[1] -= wrr_tokens_min;
2583         grinder->wrr_tokens[2] -= wrr_tokens_min;
2584         grinder->wrr_tokens[3] -= wrr_tokens_min;
2585 }
2586
2587
2588 #define grinder_evict(port, pos)
2589
2590 static inline void
2591 grinder_prefetch_pipe(struct rte_sched_port *port, uint32_t pos)
2592 {
2593         struct rte_sched_grinder *grinder = port->grinder + pos;
2594
2595         rte_prefetch0(grinder->pipe);
2596         rte_prefetch0(grinder->queue[0]);
2597 }
2598
2599 static inline void
2600 grinder_prefetch_tc_queue_arrays(struct rte_sched_port *port, uint32_t pos)
2601 {
2602         struct rte_sched_grinder *grinder = port->grinder + pos;
2603         uint16_t qsize, qr[RTE_SCHED_MAX_QUEUES_PER_TC];
2604
2605         qsize = grinder->qsize;
2606         grinder->qpos = 0;
2607
2608         if (grinder->tc_index < RTE_SCHED_TRAFFIC_CLASS_BE) {
2609                 qr[0] = grinder->queue[0]->qr & (qsize - 1);
2610
2611                 rte_prefetch0(grinder->qbase[0] + qr[0]);
2612                 return;
2613         }
2614
2615         qr[0] = grinder->queue[0]->qr & (qsize - 1);
2616         qr[1] = grinder->queue[1]->qr & (qsize - 1);
2617         qr[2] = grinder->queue[2]->qr & (qsize - 1);
2618         qr[3] = grinder->queue[3]->qr & (qsize - 1);
2619
2620         rte_prefetch0(grinder->qbase[0] + qr[0]);
2621         rte_prefetch0(grinder->qbase[1] + qr[1]);
2622
2623         grinder_wrr_load(port, pos);
2624         grinder_wrr(port, pos);
2625
2626         rte_prefetch0(grinder->qbase[2] + qr[2]);
2627         rte_prefetch0(grinder->qbase[3] + qr[3]);
2628 }
2629
2630 static inline void
2631 grinder_prefetch_mbuf(struct rte_sched_port *port, uint32_t pos)
2632 {
2633         struct rte_sched_grinder *grinder = port->grinder + pos;
2634         uint32_t qpos = grinder->qpos;
2635         struct rte_mbuf **qbase = grinder->qbase[qpos];
2636         uint16_t qsize = grinder->qsize;
2637         uint16_t qr = grinder->queue[qpos]->qr & (qsize - 1);
2638
2639         grinder->pkt = qbase[qr];
2640         rte_prefetch0(grinder->pkt);
2641
2642         if (unlikely((qr & 0x7) == 7)) {
2643                 uint16_t qr_next = (grinder->queue[qpos]->qr + 1) & (qsize - 1);
2644
2645                 rte_prefetch0(qbase + qr_next);
2646         }
2647 }
2648
2649 static inline uint32_t
2650 grinder_handle(struct rte_sched_port *port, uint32_t pos)
2651 {
2652         struct rte_sched_grinder *grinder = port->grinder + pos;
2653
2654         switch (grinder->state) {
2655         case e_GRINDER_PREFETCH_PIPE:
2656         {
2657                 if (grinder_next_pipe(port, pos)) {
2658                         grinder_prefetch_pipe(port, pos);
2659                         port->busy_grinders++;
2660
2661                         grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2662                         return 0;
2663                 }
2664
2665                 return 0;
2666         }
2667
2668         case e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS:
2669         {
2670                 struct rte_sched_pipe *pipe = grinder->pipe;
2671
2672                 grinder->pipe_params = port->pipe_profiles + pipe->profile;
2673                 grinder_prefetch_tc_queue_arrays(port, pos);
2674                 grinder_credits_update(port, pos);
2675
2676                 grinder->state = e_GRINDER_PREFETCH_MBUF;
2677                 return 0;
2678         }
2679
2680         case e_GRINDER_PREFETCH_MBUF:
2681         {
2682                 grinder_prefetch_mbuf(port, pos);
2683
2684                 grinder->state = e_GRINDER_READ_MBUF;
2685                 return 0;
2686         }
2687
2688         case e_GRINDER_READ_MBUF:
2689         {
2690                 uint32_t wrr_active, result = 0;
2691
2692                 result = grinder_schedule(port, pos);
2693
2694                 wrr_active = (grinder->tc_index == RTE_SCHED_TRAFFIC_CLASS_BE);
2695
2696                 /* Look for next packet within the same TC */
2697                 if (result && grinder->qmask) {
2698                         if (wrr_active)
2699                                 grinder_wrr(port, pos);
2700
2701                         grinder_prefetch_mbuf(port, pos);
2702
2703                         return 1;
2704                 }
2705
2706                 if (wrr_active)
2707                         grinder_wrr_store(port, pos);
2708
2709                 /* Look for another active TC within same pipe */
2710                 if (grinder_next_tc(port, pos)) {
2711                         grinder_prefetch_tc_queue_arrays(port, pos);
2712
2713                         grinder->state = e_GRINDER_PREFETCH_MBUF;
2714                         return result;
2715                 }
2716
2717                 if (grinder->productive == 0 &&
2718                     port->pipe_loop == RTE_SCHED_PIPE_INVALID)
2719                         port->pipe_loop = grinder->pindex;
2720
2721                 grinder_evict(port, pos);
2722
2723                 /* Look for another active pipe */
2724                 if (grinder_next_pipe(port, pos)) {
2725                         grinder_prefetch_pipe(port, pos);
2726
2727                         grinder->state = e_GRINDER_PREFETCH_TC_QUEUE_ARRAYS;
2728                         return result;
2729                 }
2730
2731                 /* No active pipe found */
2732                 port->busy_grinders--;
2733
2734                 grinder->state = e_GRINDER_PREFETCH_PIPE;
2735                 return result;
2736         }
2737
2738         default:
2739                 rte_panic("Algorithmic error (invalid state)\n");
2740                 return 0;
2741         }
2742 }
2743
2744 static inline void
2745 rte_sched_port_time_resync(struct rte_sched_port *port)
2746 {
2747         uint64_t cycles = rte_get_tsc_cycles();
2748         uint64_t cycles_diff = cycles - port->time_cpu_cycles;
2749         uint64_t bytes_diff;
2750
2751         /* Compute elapsed time in bytes */
2752         bytes_diff = rte_reciprocal_divide(cycles_diff << RTE_SCHED_TIME_SHIFT,
2753                                            port->inv_cycles_per_byte);
2754
2755         /* Advance port time */
2756         port->time_cpu_cycles = cycles;
2757         port->time_cpu_bytes += bytes_diff;
2758         if (port->time < port->time_cpu_bytes)
2759                 port->time = port->time_cpu_bytes;
2760
2761         /* Reset pipe loop detection */
2762         port->pipe_loop = RTE_SCHED_PIPE_INVALID;
2763 }
2764
2765 static inline int
2766 rte_sched_port_exceptions(struct rte_sched_port *port, int second_pass)
2767 {
2768         int exceptions;
2769
2770         /* Check if any exception flag is set */
2771         exceptions = (second_pass && port->busy_grinders == 0) ||
2772                 (port->pipe_exhaustion == 1);
2773
2774         /* Clear exception flags */
2775         port->pipe_exhaustion = 0;
2776
2777         return exceptions;
2778 }
2779
2780 int
2781 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts)
2782 {
2783         uint32_t i, count;
2784
2785         port->pkts_out = pkts;
2786         port->n_pkts_out = 0;
2787
2788         rte_sched_port_time_resync(port);
2789
2790         /* Take each queue in the grinder one step further */
2791         for (i = 0, count = 0; ; i++)  {
2792                 count += grinder_handle(port, i & (RTE_SCHED_PORT_N_GRINDERS - 1));
2793                 if ((count == n_pkts) ||
2794                     rte_sched_port_exceptions(port, i >= RTE_SCHED_PORT_N_GRINDERS)) {
2795                         break;
2796                 }
2797         }
2798
2799         return count;
2800 }