c0f4ad355e55355a515c18e5b99fc32e2fbfc137
[dpdk.git] / lib / librte_sched / rte_sched.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef __INCLUDE_RTE_SCHED_H__
35 #define __INCLUDE_RTE_SCHED_H__
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42  * @file
43  * RTE Hierarchical Scheduler
44  *
45  * The hierarchical scheduler prioritizes the transmission of packets
46  * from different users and traffic classes according to the Service
47  * Level Agreements (SLAs) defined for the current network node.
48  *
49  * The scheduler supports thousands of packet queues grouped under a
50  * 5-level hierarchy:
51  *     1. Port:
52  *           - Typical usage: output Ethernet port;
53  *           - Multiple ports are scheduled in round robin order with
54  *          equal priority;
55  *     2. Subport:
56  *           - Typical usage: group of users;
57  *           - Traffic shaping using the token bucket algorithm
58  *          (one bucket per subport);
59  *           - Upper limit enforced per traffic class at subport level;
60  *           - Lower priority traffic classes able to reuse subport
61  *          bandwidth currently unused by higher priority traffic
62  *          classes of the same subport;
63  *           - When any subport traffic class is oversubscribed
64  *          (configuration time event), the usage of subport member
65  *          pipes with high demand for thattraffic class pipes is
66  *          truncated to a dynamically adjusted value with no
67  *             impact to low demand pipes;
68  *     3. Pipe:
69  *           - Typical usage: individual user/subscriber;
70  *           - Traffic shaping using the token bucket algorithm
71  *          (one bucket per pipe);
72  *     4. Traffic class:
73  *           - Traffic classes of the same pipe handled in strict
74  *          priority order;
75  *           - Upper limit enforced per traffic class at the pipe level;
76  *           - Lower priority traffic classes able to reuse pipe
77  *          bandwidth currently unused by higher priority traffic
78  *          classes of the same pipe;
79  *     5. Queue:
80  *           - Typical usage: queue hosting packets from one or
81  *          multiple connections of same traffic class belonging to
82  *          the same user;
83  *           - Weighted Round Robin (WRR) is used to service the
84  *          queues within same pipe traffic class.
85  *
86  */
87
88 #include <sys/types.h>
89 #include <rte_mbuf.h>
90 #include <rte_meter.h>
91
92 /** Random Early Detection (RED) */
93 #ifdef RTE_SCHED_RED
94 #include "rte_red.h"
95 #endif
96
97 /** Number of traffic classes per pipe (as well as subport).
98  * Cannot be changed.
99  */
100 #define RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE    4
101
102 /** Number of queues per pipe traffic class. Cannot be changed. */
103 #define RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS    4
104
105 /** Number of queues per pipe. */
106 #define RTE_SCHED_QUEUES_PER_PIPE             \
107         (RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE *     \
108         RTE_SCHED_QUEUES_PER_TRAFFIC_CLASS)
109
110 /** Maximum number of pipe profiles that can be defined per port.
111  * Compile-time configurable.
112  */
113 #ifndef RTE_SCHED_PIPE_PROFILES_PER_PORT
114 #define RTE_SCHED_PIPE_PROFILES_PER_PORT      256
115 #endif
116
117 /*
118  * Ethernet framing overhead. Overhead fields per Ethernet frame:
119  * 1. Preamble:                             7 bytes;
120  * 2. Start of Frame Delimiter (SFD):       1 byte;
121  * 3. Frame Check Sequence (FCS):           4 bytes;
122  * 4. Inter Frame Gap (IFG):               12 bytes.
123  *
124  * The FCS is considered overhead only if not included in the packet
125  * length (field pkt_len of struct rte_mbuf).
126  */
127 #ifndef RTE_SCHED_FRAME_OVERHEAD_DEFAULT
128 #define RTE_SCHED_FRAME_OVERHEAD_DEFAULT      24
129 #endif
130
131 /*
132  * Subport configuration parameters. The period and credits_per_period
133  * parameters are measured in bytes, with one byte meaning the time
134  * duration associated with the transmission of one byte on the
135  * physical medium of the output port, with pipe or pipe traffic class
136  * rate (measured as percentage of output port rate) determined as
137  * credits_per_period divided by period. One credit represents one
138  * byte.
139  */
140 struct rte_sched_subport_params {
141         /* Subport token bucket */
142         uint32_t tb_rate;                /**< Rate (measured in bytes per second) */
143         uint32_t tb_size;                /**< Size (measured in credits) */
144
145         /* Subport traffic classes */
146         uint32_t tc_rate[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
147         /**< Traffic class rates (measured in bytes per second) */
148         uint32_t tc_period;
149         /**< Enforcement period for rates (measured in milliseconds) */
150 };
151
152 /** Subport statistics */
153 struct rte_sched_subport_stats {
154         /* Packets */
155         uint32_t n_pkts_tc[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
156         /**< Number of packets successfully written */
157         uint32_t n_pkts_tc_dropped[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
158         /**< Number of packets dropped */
159
160         /* Bytes */
161         uint32_t n_bytes_tc[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
162         /**< Number of bytes successfully written for each traffic class */
163         uint32_t n_bytes_tc_dropped[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
164         /**< Number of bytes dropped for each traffic class */
165 };
166
167 /*
168  * Pipe configuration parameters. The period and credits_per_period
169  * parameters are measured in bytes, with one byte meaning the time
170  * duration associated with the transmission of one byte on the
171  * physical medium of the output port, with pipe or pipe traffic class
172  * rate (measured as percentage of output port rate) determined as
173  * credits_per_period divided by period. One credit represents one
174  * byte.
175  */
176 struct rte_sched_pipe_params {
177         /* Pipe token bucket */
178         uint32_t tb_rate;                /**< Rate (measured in bytes per second) */
179         uint32_t tb_size;                /**< Size (measured in credits) */
180
181         /* Pipe traffic classes */
182         uint32_t tc_rate[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
183         /**< Traffic class rates (measured in bytes per second) */
184         uint32_t tc_period;
185         /**< Enforcement period (measured in milliseconds) */
186 #ifdef RTE_SCHED_SUBPORT_TC_OV
187         uint8_t tc_ov_weight;            /**< Weight Traffic class 3 oversubscription */
188 #endif
189
190         /* Pipe queues */
191         uint8_t  wrr_weights[RTE_SCHED_QUEUES_PER_PIPE]; /**< WRR weights */
192 };
193
194 /** Queue statistics */
195 struct rte_sched_queue_stats {
196         /* Packets */
197         uint32_t n_pkts;                 /**< Packets successfully written */
198         uint32_t n_pkts_dropped;         /**< Packets dropped */
199
200         /* Bytes */
201         uint32_t n_bytes;                /**< Bytes successfully written */
202         uint32_t n_bytes_dropped;        /**< Bytes dropped */
203 };
204
205 /** Port configuration parameters. */
206 struct rte_sched_port_params {
207         const char *name;                /**< String to be associated */
208         int socket;                      /**< CPU socket ID */
209         uint32_t rate;                   /**< Output port rate
210                                           * (measured in bytes per second) */
211         uint32_t mtu;                    /**< Maximum Ethernet frame size
212                                           * (measured in bytes).
213                                           * Should not include the framing overhead. */
214         uint32_t frame_overhead;         /**< Framing overhead per packet
215                                           * (measured in bytes) */
216         uint32_t n_subports_per_port;    /**< Number of subports */
217         uint32_t n_pipes_per_subport;    /**< Number of pipes per subport */
218         uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
219         /**< Packet queue size for each traffic class.
220          * All queues within the same pipe traffic class have the same
221          * size. Queues from different pipes serving the same traffic
222          * class have the same size. */
223         struct rte_sched_pipe_params *pipe_profiles;
224         /**< Pipe profile table.
225          * Every pipe is configured using one of the profiles from this table. */
226         uint32_t n_pipe_profiles;        /**< Profiles in the pipe profile table */
227 #ifdef RTE_SCHED_RED
228         struct rte_red_params red_params[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE][e_RTE_METER_COLORS]; /**< RED parameters */
229 #endif
230 };
231
232 /*
233  * Configuration
234  *
235  ***/
236
237 /**
238  * Hierarchical scheduler port configuration
239  *
240  * @param params
241  *   Port scheduler configuration parameter structure
242  * @return
243  *   Handle to port scheduler instance upon success or NULL otherwise.
244  */
245 struct rte_sched_port *
246 rte_sched_port_config(struct rte_sched_port_params *params);
247
248 /**
249  * Hierarchical scheduler port free
250  *
251  * @param port
252  *   Handle to port scheduler instance
253  */
254 void
255 rte_sched_port_free(struct rte_sched_port *port);
256
257 /**
258  * Hierarchical scheduler subport configuration
259  *
260  * @param port
261  *   Handle to port scheduler instance
262  * @param subport_id
263  *   Subport ID
264  * @param params
265  *   Subport configuration parameters
266  * @return
267  *   0 upon success, error code otherwise
268  */
269 int
270 rte_sched_subport_config(struct rte_sched_port *port,
271         uint32_t subport_id,
272         struct rte_sched_subport_params *params);
273
274 /**
275  * Hierarchical scheduler pipe configuration
276  *
277  * @param port
278  *   Handle to port scheduler instance
279  * @param subport_id
280  *   Subport ID
281  * @param pipe_id
282  *   Pipe ID within subport
283  * @param pipe_profile
284  *   ID of port-level pre-configured pipe profile
285  * @return
286  *   0 upon success, error code otherwise
287  */
288 int
289 rte_sched_pipe_config(struct rte_sched_port *port,
290         uint32_t subport_id,
291         uint32_t pipe_id,
292         int32_t pipe_profile);
293
294 /**
295  * Hierarchical scheduler memory footprint size per port
296  *
297  * @param params
298  *   Port scheduler configuration parameter structure
299  * @return
300  *   Memory footprint size in bytes upon success, 0 otherwise
301  */
302 uint32_t
303 rte_sched_port_get_memory_footprint(struct rte_sched_port_params *params);
304
305 /*
306  * Statistics
307  *
308  ***/
309
310 /**
311  * Hierarchical scheduler subport statistics read
312  *
313  * @param port
314  *   Handle to port scheduler instance
315  * @param subport_id
316  *   Subport ID
317  * @param stats
318  *   Pointer to pre-allocated subport statistics structure where the statistics
319  *   counters should be stored
320  * @param tc_ov
321  *   Pointer to pre-allocated 4-entry array where the oversubscription status for
322  *   each of the 4 subport traffic classes should be stored.
323  * @return
324  *   0 upon success, error code otherwise
325  */
326 int
327 rte_sched_subport_read_stats(struct rte_sched_port *port,
328         uint32_t subport_id,
329         struct rte_sched_subport_stats *stats,
330         uint32_t *tc_ov);
331
332 /**
333  * Hierarchical scheduler queue statistics read
334  *
335  * @param port
336  *   Handle to port scheduler instance
337  * @param queue_id
338  *   Queue ID within port scheduler
339  * @param stats
340  *   Pointer to pre-allocated subport statistics structure where the statistics
341  *   counters should be stored
342  * @param qlen
343  *   Pointer to pre-allocated variable where the current queue length
344  *   should be stored.
345  * @return
346  *   0 upon success, error code otherwise
347  */
348 int
349 rte_sched_queue_read_stats(struct rte_sched_port *port,
350         uint32_t queue_id,
351         struct rte_sched_queue_stats *stats,
352         uint16_t *qlen);
353
354 /**
355  * Scheduler hierarchy path write to packet descriptor. Typically
356  * called by the packet classification stage.
357  *
358  * @param pkt
359  *   Packet descriptor handle
360  * @param subport
361  *   Subport ID
362  * @param pipe
363  *   Pipe ID within subport
364  * @param traffic_class
365  *   Traffic class ID within pipe (0 .. 3)
366  * @param queue
367  *   Queue ID within pipe traffic class (0 .. 3)
368  * @param color
369  *   Packet color set
370  */
371 void
372 rte_sched_port_pkt_write(struct rte_mbuf *pkt,
373                          uint32_t subport, uint32_t pipe, uint32_t traffic_class,
374                          uint32_t queue, enum rte_meter_color color);
375
376 /**
377  * Scheduler hierarchy path read from packet descriptor (struct
378  * rte_mbuf). Typically called as part of the hierarchical scheduler
379  * enqueue operation. The subport, pipe, traffic class and queue
380  * parameters need to be pre-allocated by the caller.
381  *
382  * @param pkt
383  *   Packet descriptor handle
384  * @param subport
385  *   Subport ID
386  * @param pipe
387  *   Pipe ID within subport
388  * @param traffic_class
389  *   Traffic class ID within pipe (0 .. 3)
390  * @param queue
391  *   Queue ID within pipe traffic class (0 .. 3)
392  *
393  */
394 void
395 rte_sched_port_pkt_read_tree_path(const struct rte_mbuf *pkt,
396                                   uint32_t *subport, uint32_t *pipe,
397                                   uint32_t *traffic_class, uint32_t *queue);
398
399 enum rte_meter_color
400 rte_sched_port_pkt_read_color(const struct rte_mbuf *pkt);
401
402 /**
403  * Hierarchical scheduler port enqueue. Writes up to n_pkts to port
404  * scheduler and returns the number of packets actually written. For
405  * each packet, the port scheduler queue to write the packet to is
406  * identified by reading the hierarchy path from the packet
407  * descriptor; if the queue is full or congested and the packet is not
408  * written to the queue, then the packet is automatically dropped
409  * without any action required from the caller.
410  *
411  * @param port
412  *   Handle to port scheduler instance
413  * @param pkts
414  *   Array storing the packet descriptor handles
415  * @param n_pkts
416  *   Number of packets to enqueue from the pkts array into the port scheduler
417  * @return
418  *   Number of packets successfully enqueued
419  */
420 int
421 rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts);
422
423 /**
424  * Hierarchical scheduler port dequeue. Reads up to n_pkts from the
425  * port scheduler and stores them in the pkts array and returns the
426  * number of packets actually read.  The pkts array needs to be
427  * pre-allocated by the caller with at least n_pkts entries.
428  *
429  * @param port
430  *   Handle to port scheduler instance
431  * @param pkts
432  *   Pre-allocated packet descriptor array where the packets dequeued
433  *   from the port
434  *   scheduler should be stored
435  * @param n_pkts
436  *   Number of packets to dequeue from the port scheduler
437  * @return
438  *   Number of packets successfully dequeued and placed in the pkts array
439  */
440 int
441 rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts);
442
443 #ifdef __cplusplus
444 }
445 #endif
446
447 #endif /* __INCLUDE_RTE_SCHED_H__ */