sched: remove redundant macros
[dpdk.git] / lib / librte_sched / rte_sched.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef __INCLUDE_RTE_SCHED_H__
6 #define __INCLUDE_RTE_SCHED_H__
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 /**
13  * @file
14  * RTE Hierarchical Scheduler
15  *
16  * The hierarchical scheduler prioritizes the transmission of packets
17  * from different users and traffic classes according to the Service
18  * Level Agreements (SLAs) defined for the current network node.
19  *
20  * The scheduler supports thousands of packet queues grouped under a
21  * 5-level hierarchy:
22  *     1. Port:
23  *           - Typical usage: output Ethernet port;
24  *           - Multiple ports are scheduled in round robin order with
25  *          equal priority;
26  *     2. Subport:
27  *           - Typical usage: group of users;
28  *           - Traffic shaping using the token bucket algorithm
29  *          (one bucket per subport);
30  *           - Upper limit enforced per traffic class at subport level;
31  *           - Lower priority traffic classes able to reuse subport
32  *          bandwidth currently unused by higher priority traffic
33  *          classes of the same subport;
34  *           - When any subport traffic class is oversubscribed
35  *          (configuration time event), the usage of subport member
36  *          pipes with high demand for that traffic class pipes is
37  *          truncated to a dynamically adjusted value with no
38  *             impact to low demand pipes;
39  *     3. Pipe:
40  *           - Typical usage: individual user/subscriber;
41  *           - Traffic shaping using the token bucket algorithm
42  *          (one bucket per pipe);
43  *     4. Traffic class:
44  *           - Traffic classes of the same pipe handled in strict
45  *          priority order;
46  *           - Upper limit enforced per traffic class at the pipe level;
47  *           - Lower priority traffic classes able to reuse pipe
48  *          bandwidth currently unused by higher priority traffic
49  *          classes of the same pipe;
50  *     5. Queue:
51  *           - Typical usage: queue hosting packets from one or
52  *          multiple connections of same traffic class belonging to
53  *          the same user;
54  *           - Weighted Round Robin (WRR) is used to service the
55  *          queues within same pipe lowest priority traffic class (best-effort).
56  *
57  */
58
59 #include <sys/types.h>
60 #include <rte_compat.h>
61 #include <rte_mbuf.h>
62 #include <rte_meter.h>
63
64 /** Random Early Detection (RED) */
65 #ifdef RTE_SCHED_RED
66 #include "rte_red.h"
67 #endif
68
69 /** Maximum number of queues per pipe.
70  * Note that the multiple queues (power of 2) can only be assigned to
71  * lowest priority (best-effort) traffic class. Other higher priority traffic
72  * classes can only have one queue.
73  * Can not change.
74  *
75  * @see struct rte_sched_port_params
76  */
77 #define RTE_SCHED_QUEUES_PER_PIPE    16
78
79 /** Number of WRR queues for best-effort traffic class per pipe.
80  *
81  * @see struct rte_sched_pipe_params
82  */
83 #define RTE_SCHED_BE_QUEUES_PER_PIPE    4
84
85 /** Number of traffic classes per pipe (as well as subport).
86  * @see struct rte_sched_subport_params
87  * @see struct rte_sched_pipe_params
88  */
89 #define RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE    \
90 (RTE_SCHED_QUEUES_PER_PIPE - RTE_SCHED_BE_QUEUES_PER_PIPE + 1)
91
92 /** Best-effort traffic class ID
93  * Can not change.
94  */
95 #define RTE_SCHED_TRAFFIC_CLASS_BE    (RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE - 1)
96
97 /*
98  * Ethernet framing overhead. Overhead fields per Ethernet frame:
99  * 1. Preamble:                             7 bytes;
100  * 2. Start of Frame Delimiter (SFD):       1 byte;
101  * 3. Frame Check Sequence (FCS):           4 bytes;
102  * 4. Inter Frame Gap (IFG):               12 bytes.
103  *
104  * The FCS is considered overhead only if not included in the packet
105  * length (field pkt_len of struct rte_mbuf).
106  *
107  * @see struct rte_sched_port_params
108  */
109 #ifndef RTE_SCHED_FRAME_OVERHEAD_DEFAULT
110 #define RTE_SCHED_FRAME_OVERHEAD_DEFAULT      24
111 #endif
112
113 /*
114  * Subport configuration parameters. The period and credits_per_period
115  * parameters are measured in bytes, with one byte meaning the time
116  * duration associated with the transmission of one byte on the
117  * physical medium of the output port, with pipe or pipe traffic class
118  * rate (measured as percentage of output port rate) determined as
119  * credits_per_period divided by period. One credit represents one
120  * byte.
121  */
122 struct rte_sched_subport_params {
123         /** Token bucket rate (measured in bytes per second) */
124         uint32_t tb_rate;
125
126         /** Token bucket size (measured in credits) */
127         uint32_t tb_size;
128
129         /** Traffic class rates (measured in bytes per second) */
130         uint32_t tc_rate[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
131
132         /** Enforcement period for rates (measured in milliseconds) */
133         uint32_t tc_period;
134 };
135
136 /** Subport statistics */
137 struct rte_sched_subport_stats {
138         /** Number of packets successfully written */
139         uint32_t n_pkts_tc[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
140
141         /** Number of packets dropped */
142         uint32_t n_pkts_tc_dropped[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
143
144         /** Number of bytes successfully written for each traffic class */
145         uint32_t n_bytes_tc[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
146
147         /** Number of bytes dropped for each traffic class */
148         uint32_t n_bytes_tc_dropped[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
149
150 #ifdef RTE_SCHED_RED
151         /** Number of packets dropped by red */
152         uint32_t n_pkts_red_dropped[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
153 #endif
154 };
155
156 /*
157  * Pipe configuration parameters. The period and credits_per_period
158  * parameters are measured in bytes, with one byte meaning the time
159  * duration associated with the transmission of one byte on the
160  * physical medium of the output port, with pipe or pipe traffic class
161  * rate (measured as percentage of output port rate) determined as
162  * credits_per_period divided by period. One credit represents one
163  * byte.
164  */
165 struct rte_sched_pipe_params {
166         /** Token bucket rate (measured in bytes per second) */
167         uint32_t tb_rate;
168
169         /** Token bucket size (measured in credits) */
170         uint32_t tb_size;
171
172         /** Traffic class rates (measured in bytes per second) */
173         uint32_t tc_rate[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
174
175         /** Enforcement period (measured in milliseconds) */
176         uint32_t tc_period;
177
178         /** Best-effort traffic class oversubscription weight */
179         uint8_t tc_ov_weight;
180
181         /** WRR weights of best-effort traffic class queues */
182         uint8_t wrr_weights[RTE_SCHED_BE_QUEUES_PER_PIPE];
183 };
184
185 /** Queue statistics */
186 struct rte_sched_queue_stats {
187         /** Packets successfully written */
188         uint32_t n_pkts;
189
190         /** Packets dropped */
191         uint32_t n_pkts_dropped;
192
193 #ifdef RTE_SCHED_RED
194         /** Packets dropped by RED */
195         uint32_t n_pkts_red_dropped;
196 #endif
197
198         /** Bytes successfully written */
199         uint32_t n_bytes;
200
201         /** Bytes dropped */
202         uint32_t n_bytes_dropped;
203 };
204
205 /** Port configuration parameters. */
206 struct rte_sched_port_params {
207         /** Name of the port to be associated */
208         const char *name;
209
210         /** CPU socket ID */
211         int socket;
212
213         /** Output port rate (measured in bytes per second) */
214         uint32_t rate;
215
216         /** Maximum Ethernet frame size (measured in bytes).
217          * Should not include the framing overhead.
218          */
219         uint32_t mtu;
220
221         /** Framing overhead per packet (measured in bytes) */
222         uint32_t frame_overhead;
223
224         /** Number of subports */
225         uint32_t n_subports_per_port;
226
227         /** Number of subport_pipes */
228         uint32_t n_pipes_per_subport;
229
230         /** Packet queue size for each traffic class.
231          * All the pipes within the same subport share the similar
232          * configuration for the queues.
233          */
234         uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
235
236         /** Pipe profile table.
237          * Every pipe is configured using one of the profiles from this table.
238          */
239         struct rte_sched_pipe_params *pipe_profiles;
240
241         /** Profiles in the pipe profile table */
242         uint32_t n_pipe_profiles;
243
244         /** Max profiles allowed in the pipe profile table */
245         uint32_t n_max_pipe_profiles;
246
247 #ifdef RTE_SCHED_RED
248         /** RED parameters */
249         struct rte_red_params red_params[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS];
250 #endif
251 };
252
253 /*
254  * Configuration
255  *
256  ***/
257
258 /**
259  * Hierarchical scheduler port configuration
260  *
261  * @param params
262  *   Port scheduler configuration parameter structure
263  * @return
264  *   Handle to port scheduler instance upon success or NULL otherwise.
265  */
266 struct rte_sched_port *
267 rte_sched_port_config(struct rte_sched_port_params *params);
268
269 /**
270  * Hierarchical scheduler port free
271  *
272  * @param port
273  *   Handle to port scheduler instance
274  */
275 void
276 rte_sched_port_free(struct rte_sched_port *port);
277
278 /**
279  * @warning
280  * @b EXPERIMENTAL: this API may change without prior notice.
281  *
282  * Hierarchical scheduler pipe profile add
283  *
284  * @param port
285  *   Handle to port scheduler instance
286  * @param params
287  *   Pipe profile parameters
288  * @param pipe_profile_id
289  *   Set to valid profile id when profile is added successfully.
290  * @return
291  *   0 upon success, error code otherwise
292  */
293 __rte_experimental
294 int
295 rte_sched_port_pipe_profile_add(struct rte_sched_port *port,
296         struct rte_sched_pipe_params *params,
297         uint32_t *pipe_profile_id);
298
299 /**
300  * Hierarchical scheduler subport configuration
301  *
302  * @param port
303  *   Handle to port scheduler instance
304  * @param subport_id
305  *   Subport ID
306  * @param params
307  *   Subport configuration parameters
308  * @return
309  *   0 upon success, error code otherwise
310  */
311 int
312 rte_sched_subport_config(struct rte_sched_port *port,
313         uint32_t subport_id,
314         struct rte_sched_subport_params *params);
315
316 /**
317  * Hierarchical scheduler pipe configuration
318  *
319  * @param port
320  *   Handle to port scheduler instance
321  * @param subport_id
322  *   Subport ID
323  * @param pipe_id
324  *   Pipe ID within subport
325  * @param pipe_profile
326  *   ID of port-level pre-configured pipe profile
327  * @return
328  *   0 upon success, error code otherwise
329  */
330 int
331 rte_sched_pipe_config(struct rte_sched_port *port,
332         uint32_t subport_id,
333         uint32_t pipe_id,
334         int32_t pipe_profile);
335
336 /**
337  * Hierarchical scheduler memory footprint size per port
338  *
339  * @param params
340  *   Port scheduler configuration parameter structure
341  * @return
342  *   Memory footprint size in bytes upon success, 0 otherwise
343  */
344 uint32_t
345 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params);
346
347 /*
348  * Statistics
349  *
350  ***/
351
352 /**
353  * Hierarchical scheduler subport statistics read
354  *
355  * @param port
356  *   Handle to port scheduler instance
357  * @param subport_id
358  *   Subport ID
359  * @param stats
360  *   Pointer to pre-allocated subport statistics structure where the statistics
361  *   counters should be stored
362  * @param tc_ov
363  *   Pointer to pre-allocated RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE-entry array
364  *   where the oversubscription status for each of the subport traffic classes
365  *   should be stored.
366  * @return
367  *   0 upon success, error code otherwise
368  */
369 int
370 rte_sched_subport_read_stats(struct rte_sched_port *port,
371         uint32_t subport_id,
372         struct rte_sched_subport_stats *stats,
373         uint32_t *tc_ov);
374
375 /**
376  * Hierarchical scheduler queue statistics read
377  *
378  * @param port
379  *   Handle to port scheduler instance
380  * @param queue_id
381  *   Queue ID within port scheduler
382  * @param stats
383  *   Pointer to pre-allocated subport statistics structure where the statistics
384  *   counters should be stored
385  * @param qlen
386  *   Pointer to pre-allocated variable where the current queue length
387  *   should be stored.
388  * @return
389  *   0 upon success, error code otherwise
390  */
391 int
392 rte_sched_queue_read_stats(struct rte_sched_port *port,
393         uint32_t queue_id,
394         struct rte_sched_queue_stats *stats,
395         uint16_t *qlen);
396
397 /**
398  * Scheduler hierarchy path write to packet descriptor. Typically
399  * called by the packet classification stage.
400  *
401  * @param port
402  *   Handle to port scheduler instance
403  * @param pkt
404  *   Packet descriptor handle
405  * @param subport
406  *   Subport ID
407  * @param pipe
408  *   Pipe ID within subport
409  * @param traffic_class
410  *   Traffic class ID within pipe (0 .. RTE_SCHED_TRAFFIC_CLASS_BE)
411  * @param queue
412  *   Queue ID within pipe traffic class, 0 for high priority TCs, and
413  *   0 .. (RTE_SCHED_BE_QUEUES_PER_PIPE - 1) for best-effort TC
414  * @param color
415  *   Packet color set
416  */
417 void
418 rte_sched_port_pkt_write(struct rte_sched_port *port,
419                          struct rte_mbuf *pkt,
420                          uint32_t subport, uint32_t pipe, uint32_t traffic_class,
421                          uint32_t queue, enum rte_color color);
422
423 /**
424  * Scheduler hierarchy path read from packet descriptor (struct
425  * rte_mbuf). Typically called as part of the hierarchical scheduler
426  * enqueue operation. The subport, pipe, traffic class and queue
427  * parameters need to be pre-allocated by the caller.
428  *
429  * @param port
430  *   Handle to port scheduler instance
431  * @param pkt
432  *   Packet descriptor handle
433  * @param subport
434  *   Subport ID
435  * @param pipe
436  *   Pipe ID within subport
437  * @param traffic_class
438  *   Traffic class ID within pipe (0 .. RTE_SCHED_TRAFFIC_CLASS_BE)
439  * @param queue
440  *   Queue ID within pipe traffic class, 0 for high priority TCs, and
441  *   0 .. (RTE_SCHED_BE_QUEUES_PER_PIPE - 1) for best-effort TC
442  */
443 void
444 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
445                                   const struct rte_mbuf *pkt,
446                                   uint32_t *subport, uint32_t *pipe,
447                                   uint32_t *traffic_class, uint32_t *queue);
448
449 enum rte_color
450 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt);
451
452 /**
453  * Hierarchical scheduler port enqueue. Writes up to n_pkts to port
454  * scheduler and returns the number of packets actually written. For
455  * each packet, the port scheduler queue to write the packet to is
456  * identified by reading the hierarchy path from the packet
457  * descriptor; if the queue is full or congested and the packet is not
458  * written to the queue, then the packet is automatically dropped
459  * without any action required from the caller.
460  *
461  * @param port
462  *   Handle to port scheduler instance
463  * @param pkts
464  *   Array storing the packet descriptor handles
465  * @param n_pkts
466  *   Number of packets to enqueue from the pkts array into the port scheduler
467  * @return
468  *   Number of packets successfully enqueued
469  */
470 int
471 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts);
472
473 /**
474  * Hierarchical scheduler port dequeue. Reads up to n_pkts from the
475  * port scheduler and stores them in the pkts array and returns the
476  * number of packets actually read.  The pkts array needs to be
477  * pre-allocated by the caller with at least n_pkts entries.
478  *
479  * @param port
480  *   Handle to port scheduler instance
481  * @param pkts
482  *   Pre-allocated packet descriptor array where the packets dequeued
483  *   from the port
484  *   scheduler should be stored
485  * @param n_pkts
486  *   Number of packets to dequeue from the port scheduler
487  * @return
488  *   Number of packets successfully dequeued and placed in the pkts array
489  */
490 int
491 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts);
492
493 #ifdef __cplusplus
494 }
495 #endif
496
497 #endif /* __INCLUDE_RTE_SCHED_H__ */