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