sched: add config flexibility to TC queue sizes
[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 traffic class.
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  * Cannot be changed.
87  */
88 #define RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE    \
89 (RTE_SCHED_QUEUES_PER_PIPE - RTE_SCHED_BE_QUEUES_PER_PIPE + 1)
90
91 /** Best-effort traffic class ID
92  * Can not change.
93  */
94 #define RTE_SCHED_TRAFFIC_CLASS_BE    (RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE - 1)
95
96 /** Number of queues per pipe traffic class. Cannot be changed. */
97 #define RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS    4
98
99
100 /** Maximum number of pipe profiles that can be defined per port.
101  * Compile-time configurable.
102  */
103 #ifndef RTE_SCHED_PIPE_PROFILES_PER_PORT
104 #define RTE_SCHED_PIPE_PROFILES_PER_PORT      256
105 #endif
106
107 /*
108  * Ethernet framing overhead. Overhead fields per Ethernet frame:
109  * 1. Preamble:                             7 bytes;
110  * 2. Start of Frame Delimiter (SFD):       1 byte;
111  * 3. Frame Check Sequence (FCS):           4 bytes;
112  * 4. Inter Frame Gap (IFG):               12 bytes.
113  *
114  * The FCS is considered overhead only if not included in the packet
115  * length (field pkt_len of struct rte_mbuf).
116  */
117 #ifndef RTE_SCHED_FRAME_OVERHEAD_DEFAULT
118 #define RTE_SCHED_FRAME_OVERHEAD_DEFAULT      24
119 #endif
120
121 /*
122  * Subport configuration parameters. The period and credits_per_period
123  * parameters are measured in bytes, with one byte meaning the time
124  * duration associated with the transmission of one byte on the
125  * physical medium of the output port, with pipe or pipe traffic class
126  * rate (measured as percentage of output port rate) determined as
127  * credits_per_period divided by period. One credit represents one
128  * byte.
129  */
130 struct rte_sched_subport_params {
131         /* Subport token bucket */
132         uint32_t tb_rate;                /**< Rate (measured in bytes per second) */
133         uint32_t tb_size;                /**< Size (measured in credits) */
134
135         /* Subport traffic classes */
136         uint32_t tc_rate[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
137         /**< Traffic class rates (measured in bytes per second) */
138         uint32_t tc_period;
139         /**< Enforcement period for rates (measured in milliseconds) */
140 };
141
142 /** Subport statistics */
143 struct rte_sched_subport_stats {
144         /* Packets */
145         uint32_t n_pkts_tc[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
146         /**< Number of packets successfully written */
147         uint32_t n_pkts_tc_dropped[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
148         /**< Number of packets dropped */
149
150         /* Bytes */
151         uint32_t n_bytes_tc[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
152         /**< Number of bytes successfully written for each traffic class */
153         uint32_t n_bytes_tc_dropped[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
154         /**< Number of bytes dropped for each traffic class */
155
156 #ifdef RTE_SCHED_RED
157         uint32_t n_pkts_red_dropped[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
158         /**< Number of packets dropped by red */
159 #endif
160 };
161
162 /*
163  * Pipe configuration parameters. The period and credits_per_period
164  * parameters are measured in bytes, with one byte meaning the time
165  * duration associated with the transmission of one byte on the
166  * physical medium of the output port, with pipe or pipe traffic class
167  * rate (measured as percentage of output port rate) determined as
168  * credits_per_period divided by period. One credit represents one
169  * byte.
170  */
171 struct rte_sched_pipe_params {
172         /* Pipe token bucket */
173         uint32_t tb_rate;                /**< Rate (measured in bytes per second) */
174         uint32_t tb_size;                /**< Size (measured in credits) */
175
176         /* Pipe traffic classes */
177         uint32_t tc_rate[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
178         /**< Traffic class rates (measured in bytes per second) */
179         uint32_t tc_period;
180         /**< Enforcement period (measured in milliseconds) */
181         uint8_t tc_ov_weight;            /**< Weight Traffic class 3 oversubscription */
182
183         /* Pipe queues */
184         uint8_t  wrr_weights[RTE_SCHED_BE_QUEUES_PER_PIPE]; /**< WRR weights */
185 };
186
187 /** Queue statistics */
188 struct rte_sched_queue_stats {
189         /* Packets */
190         uint32_t n_pkts;                 /**< Packets successfully written */
191         uint32_t n_pkts_dropped;         /**< Packets dropped */
192 #ifdef RTE_SCHED_RED
193         uint32_t n_pkts_red_dropped;     /**< Packets dropped by RED */
194 #endif
195
196         /* Bytes */
197         uint32_t n_bytes;                /**< Bytes successfully written */
198         uint32_t n_bytes_dropped;        /**< Bytes dropped */
199 };
200
201 /** Port configuration parameters. */
202 struct rte_sched_port_params {
203         const char *name;                /**< String to be associated */
204         int socket;                      /**< CPU socket ID */
205         uint32_t rate;                   /**< Output port rate
206                                           * (measured in bytes per second) */
207         uint32_t mtu;                    /**< Maximum Ethernet frame size
208                                           * (measured in bytes).
209                                           * Should not include the framing overhead. */
210         uint32_t frame_overhead;         /**< Framing overhead per packet
211                                           * (measured in bytes) */
212         uint32_t n_subports_per_port;    /**< Number of subports */
213         uint32_t n_pipes_per_subport;    /**< Number of pipes per subport */
214         uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
215         /**< Packet queue size for each traffic class.
216          * All queues within the same pipe traffic class have the same
217          * size. Queues from different pipes serving the same traffic
218          * class have the same size. */
219         struct rte_sched_pipe_params *pipe_profiles;
220         /**< Pipe profile table.
221          * Every pipe is configured using one of the profiles from this table. */
222         uint32_t n_pipe_profiles;        /**< Profiles in the pipe profile table */
223 #ifdef RTE_SCHED_RED
224         struct rte_red_params red_params[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][RTE_COLORS]; /**< RED parameters */
225 #endif
226 };
227
228 /*
229  * Configuration
230  *
231  ***/
232
233 /**
234  * Hierarchical scheduler port configuration
235  *
236  * @param params
237  *   Port scheduler configuration parameter structure
238  * @return
239  *   Handle to port scheduler instance upon success or NULL otherwise.
240  */
241 struct rte_sched_port *
242 rte_sched_port_config(struct rte_sched_port_params *params);
243
244 /**
245  * Hierarchical scheduler port free
246  *
247  * @param port
248  *   Handle to port scheduler instance
249  */
250 void
251 rte_sched_port_free(struct rte_sched_port *port);
252
253 /**
254  * @warning
255  * @b EXPERIMENTAL: this API may change without prior notice.
256  *
257  * Hierarchical scheduler pipe profile add
258  *
259  * @param port
260  *   Handle to port scheduler instance
261  * @param params
262  *   Pipe profile parameters
263  * @param pipe_profile_id
264  *   Set to valid profile id when profile is added successfully.
265  * @return
266  *   0 upon success, error code otherwise
267  */
268 __rte_experimental
269 int
270 rte_sched_port_pipe_profile_add(struct rte_sched_port *port,
271         struct rte_sched_pipe_params *params,
272         uint32_t *pipe_profile_id);
273
274 /**
275  * Hierarchical scheduler subport configuration
276  *
277  * @param port
278  *   Handle to port scheduler instance
279  * @param subport_id
280  *   Subport ID
281  * @param params
282  *   Subport configuration parameters
283  * @return
284  *   0 upon success, error code otherwise
285  */
286 int
287 rte_sched_subport_config(struct rte_sched_port *port,
288         uint32_t subport_id,
289         struct rte_sched_subport_params *params);
290
291 /**
292  * Hierarchical scheduler pipe configuration
293  *
294  * @param port
295  *   Handle to port scheduler instance
296  * @param subport_id
297  *   Subport ID
298  * @param pipe_id
299  *   Pipe ID within subport
300  * @param pipe_profile
301  *   ID of port-level pre-configured pipe profile
302  * @return
303  *   0 upon success, error code otherwise
304  */
305 int
306 rte_sched_pipe_config(struct rte_sched_port *port,
307         uint32_t subport_id,
308         uint32_t pipe_id,
309         int32_t pipe_profile);
310
311 /**
312  * Hierarchical scheduler memory footprint size per port
313  *
314  * @param params
315  *   Port scheduler configuration parameter structure
316  * @return
317  *   Memory footprint size in bytes upon success, 0 otherwise
318  */
319 uint32_t
320 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params);
321
322 /*
323  * Statistics
324  *
325  ***/
326
327 /**
328  * Hierarchical scheduler subport statistics read
329  *
330  * @param port
331  *   Handle to port scheduler instance
332  * @param subport_id
333  *   Subport ID
334  * @param stats
335  *   Pointer to pre-allocated subport statistics structure where the statistics
336  *   counters should be stored
337  * @param tc_ov
338  *   Pointer to pre-allocated 4-entry array where the oversubscription status for
339  *   each of the 4 subport traffic classes should be stored.
340  * @return
341  *   0 upon success, error code otherwise
342  */
343 int
344 rte_sched_subport_read_stats(struct rte_sched_port *port,
345         uint32_t subport_id,
346         struct rte_sched_subport_stats *stats,
347         uint32_t *tc_ov);
348
349 /**
350  * Hierarchical scheduler queue statistics read
351  *
352  * @param port
353  *   Handle to port scheduler instance
354  * @param queue_id
355  *   Queue ID within port scheduler
356  * @param stats
357  *   Pointer to pre-allocated subport statistics structure where the statistics
358  *   counters should be stored
359  * @param qlen
360  *   Pointer to pre-allocated variable where the current queue length
361  *   should be stored.
362  * @return
363  *   0 upon success, error code otherwise
364  */
365 int
366 rte_sched_queue_read_stats(struct rte_sched_port *port,
367         uint32_t queue_id,
368         struct rte_sched_queue_stats *stats,
369         uint16_t *qlen);
370
371 /**
372  * Scheduler hierarchy path write to packet descriptor. Typically
373  * called by the packet classification stage.
374  *
375  * @param port
376  *   Handle to port scheduler instance
377  * @param pkt
378  *   Packet descriptor handle
379  * @param subport
380  *   Subport ID
381  * @param pipe
382  *   Pipe ID within subport
383  * @param traffic_class
384  *   Traffic class ID within pipe (0 .. 3)
385  * @param queue
386  *   Queue ID within pipe traffic class (0 .. 3)
387  * @param color
388  *   Packet color set
389  */
390 void
391 rte_sched_port_pkt_write(struct rte_sched_port *port,
392                          struct rte_mbuf *pkt,
393                          uint32_t subport, uint32_t pipe, uint32_t traffic_class,
394                          uint32_t queue, enum rte_color color);
395
396 /**
397  * Scheduler hierarchy path read from packet descriptor (struct
398  * rte_mbuf). Typically called as part of the hierarchical scheduler
399  * enqueue operation. The subport, pipe, traffic class and queue
400  * parameters need to be pre-allocated by the caller.
401  *
402  * @param port
403  *   Handle to port scheduler instance
404  * @param pkt
405  *   Packet descriptor handle
406  * @param subport
407  *   Subport ID
408  * @param pipe
409  *   Pipe ID within subport
410  * @param traffic_class
411  *   Traffic class ID within pipe (0 .. 3)
412  * @param queue
413  *   Queue ID within pipe traffic class (0 .. 3)
414  *
415  */
416 void
417 rte_sched_port_pkt_read_tree_path(struct rte_sched_port *port,
418                                   const struct rte_mbuf *pkt,
419                                   uint32_t *subport, uint32_t *pipe,
420                                   uint32_t *traffic_class, uint32_t *queue);
421
422 enum rte_color
423 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt);
424
425 /**
426  * Hierarchical scheduler port enqueue. Writes up to n_pkts to port
427  * scheduler and returns the number of packets actually written. For
428  * each packet, the port scheduler queue to write the packet to is
429  * identified by reading the hierarchy path from the packet
430  * descriptor; if the queue is full or congested and the packet is not
431  * written to the queue, then the packet is automatically dropped
432  * without any action required from the caller.
433  *
434  * @param port
435  *   Handle to port scheduler instance
436  * @param pkts
437  *   Array storing the packet descriptor handles
438  * @param n_pkts
439  *   Number of packets to enqueue from the pkts array into the port scheduler
440  * @return
441  *   Number of packets successfully enqueued
442  */
443 int
444 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts);
445
446 /**
447  * Hierarchical scheduler port dequeue. Reads up to n_pkts from the
448  * port scheduler and stores them in the pkts array and returns the
449  * number of packets actually read.  The pkts array needs to be
450  * pre-allocated by the caller with at least n_pkts entries.
451  *
452  * @param port
453  *   Handle to port scheduler instance
454  * @param pkts
455  *   Pre-allocated packet descriptor array where the packets dequeued
456  *   from the port
457  *   scheduler should be stored
458  * @param n_pkts
459  *   Number of packets to dequeue from the port scheduler
460  * @return
461  *   Number of packets successfully dequeued and placed in the pkts array
462  */
463 int
464 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts);
465
466 #ifdef __cplusplus
467 }
468 #endif
469
470 #endif /* __INCLUDE_RTE_SCHED_H__ */