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