net/ice: load OS default package
[dpdk.git] / doc / guides / prog_guide / qos_framework.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright(c) 2010-2014 Intel Corporation.
3
4 Quality of Service (QoS) Framework
5 ==================================
6
7 This chapter describes the DPDK Quality of Service (QoS) framework.
8
9 Packet Pipeline with QoS Support
10 --------------------------------
11
12 An example of a complex packet processing pipeline with QoS support is shown in the following figure.
13
14 .. _figure_pkt_proc_pipeline_qos:
15
16 .. figure:: img/pkt_proc_pipeline_qos.*
17
18    Complex Packet Processing Pipeline with QoS Support
19
20
21 This pipeline can be built using reusable DPDK software libraries.
22 The main blocks implementing QoS in this pipeline are: the policer, the dropper and the scheduler.
23 A functional description of each block is provided in the following table.
24
25 .. _table_qos_1:
26
27 .. table:: Packet Processing Pipeline Implementing QoS
28
29    +---+------------------------+--------------------------------------------------------------------------------+
30    | # | Block                  | Functional Description                                                         |
31    |   |                        |                                                                                |
32    +===+========================+================================================================================+
33    | 1 | Packet I/O RX & TX     | Packet reception/ transmission from/to multiple NIC ports. Poll mode drivers   |
34    |   |                        | (PMDs) for Intel 1 GbE/10 GbE NICs.                                            |
35    |   |                        |                                                                                |
36    +---+------------------------+--------------------------------------------------------------------------------+
37    | 2 | Packet parser          | Identify the protocol stack of the input packet. Check the integrity of the    |
38    |   |                        | packet headers.                                                                |
39    |   |                        |                                                                                |
40    +---+------------------------+--------------------------------------------------------------------------------+
41    | 3 | Flow classification    | Map the input packet to one of the known traffic flows. Exact match table      |
42    |   |                        | lookup using configurable hash function (jhash, CRC and so on) and bucket      |
43    |   |                        | logic to handle collisions.                                                    |
44    |   |                        |                                                                                |
45    +---+------------------------+--------------------------------------------------------------------------------+
46    | 4 | Policer                | Packet metering using srTCM (RFC 2697) or trTCM (RFC2698) algorithms.          |
47    |   |                        |                                                                                |
48    +---+------------------------+--------------------------------------------------------------------------------+
49    | 5 | Load Balancer          | Distribute the input packets to the application workers. Provide uniform load  |
50    |   |                        | to each worker. Preserve the affinity of traffic flows to workers and the      |
51    |   |                        | packet order within each flow.                                                 |
52    |   |                        |                                                                                |
53    +---+------------------------+--------------------------------------------------------------------------------+
54    | 6 | Worker threads         | Placeholders for the customer specific application workload (for example, IP   |
55    |   |                        | stack and so on).                                                              |
56    |   |                        |                                                                                |
57    +---+------------------------+--------------------------------------------------------------------------------+
58    | 7 | Dropper                | Congestion management using the Random Early Detection (RED) algorithm         |
59    |   |                        | (specified by the Sally Floyd - Van Jacobson paper) or Weighted RED (WRED).    |
60    |   |                        | Drop packets based on the current scheduler queue load level and packet        |
61    |   |                        | priority. When congestion is experienced, lower priority packets are dropped   |
62    |   |                        | first.                                                                         |
63    |   |                        |                                                                                |
64    +---+------------------------+--------------------------------------------------------------------------------+
65    | 8 | Hierarchical Scheduler | 5-level hierarchical scheduler (levels are: output port, subport, pipe,        |
66    |   |                        | traffic class and queue) with thousands (typically 64K) leaf nodes (queues).   |
67    |   |                        | Implements traffic shaping (for subport and pipe levels), strict priority      |
68    |   |                        | (for traffic class level) and Weighted Round Robin (WRR) (for queues within    |
69    |   |                        | each pipe traffic class).                                                      |
70    |   |                        |                                                                                |
71    +---+------------------------+--------------------------------------------------------------------------------+
72
73 The infrastructure blocks used throughout the packet processing pipeline are listed in the following table.
74
75 .. _table_qos_2:
76
77 .. table:: Infrastructure Blocks Used by the Packet Processing Pipeline
78
79    +---+-----------------------+-----------------------------------------------------------------------+
80    | # | Block                 | Functional Description                                                |
81    |   |                       |                                                                       |
82    +===+=======================+=======================================================================+
83    | 1 | Buffer manager        | Support for global buffer pools and private per-thread buffer caches. |
84    |   |                       |                                                                       |
85    +---+-----------------------+-----------------------------------------------------------------------+
86    | 2 | Queue manager         | Support for message passing between pipeline blocks.                  |
87    |   |                       |                                                                       |
88    +---+-----------------------+-----------------------------------------------------------------------+
89    | 3 | Power saving          | Support for power saving during low activity periods.                 |
90    |   |                       |                                                                       |
91    +---+-----------------------+-----------------------------------------------------------------------+
92
93 The mapping of pipeline blocks to CPU cores is configurable based on the performance level required by each specific application
94 and the set of features enabled for each block.
95 Some blocks might consume more than one CPU core (with each CPU core running a different instance of the same block on different input packets),
96 while several other blocks could be mapped to the same CPU core.
97
98 Hierarchical Scheduler
99 ----------------------
100
101 The hierarchical scheduler block, when present, usually sits on the TX side just before the transmission stage.
102 Its purpose is to prioritize the transmission of packets from different users and different traffic classes
103 according to the policy specified by the Service Level Agreements (SLAs) of each network node.
104
105 Overview
106 ~~~~~~~~
107
108 The hierarchical scheduler block is similar to the traffic manager block used by network processors
109 that typically implement per flow (or per group of flows) packet queuing and scheduling.
110 It typically acts like a buffer that is able to temporarily store a large number of packets just before their transmission (enqueue operation);
111 as the NIC TX is requesting more packets for transmission,
112 these packets are later on removed and handed over to the NIC TX with the packet selection logic observing the predefined SLAs (dequeue operation).
113
114 .. _figure_hier_sched_blk:
115
116 .. figure:: img/hier_sched_blk.*
117
118    Hierarchical Scheduler Block Internal Diagram
119
120
121 The hierarchical scheduler is optimized for a large number of packet queues.
122 When only a small number of queues are needed, message passing queues should be used instead of this block.
123 See `Worst Case Scenarios for Performance`_ for a more detailed discussion.
124
125 Scheduling Hierarchy
126 ~~~~~~~~~~~~~~~~~~~~
127
128 The scheduling hierarchy is shown in :numref:`figure_sched_hier_per_port`.
129 The first level of the hierarchy is the Ethernet TX port 1/10/40 GbE,
130 with subsequent hierarchy levels defined as subport, pipe, traffic class and queue.
131
132 Typically, each subport represents a predefined group of users, while each pipe represents an individual user/subscriber.
133 Each traffic class is the representation of a different traffic type with specific loss rate,
134 delay and jitter requirements, such as voice, video or data transfers.
135 Each queue hosts packets from one or multiple connections of the same type belonging to the same user.
136
137 .. _figure_sched_hier_per_port:
138
139 .. figure:: img/sched_hier_per_port.*
140
141    Scheduling Hierarchy per Port
142
143
144 The functionality of each hierarchical level is detailed in the following table.
145
146 .. _table_qos_3:
147
148 .. table:: Port Scheduling Hierarchy
149
150    +---+--------------------+----------------------------+---------------------------------------------------------------+
151    | # | Level              | Siblings per Parent        | Functional Description                                        |
152    |   |                    |                            |                                                               |
153    +===+====================+============================+===============================================================+
154    | 1 | Port               | -                          | #.  Output Ethernet port 1/10/40 GbE.                         |
155    |   |                    |                            |                                                               |
156    |   |                    |                            | #.  Multiple ports are scheduled in round robin order with    |
157    |   |                    |                            |     all ports having equal priority.                          |
158    |   |                    |                            |                                                               |
159    +---+--------------------+----------------------------+---------------------------------------------------------------+
160    | 2 | Subport            | Configurable (default: 8)  | #.  Traffic shaping using token bucket algorithm (one token   |
161    |   |                    |                            |     bucket per subport).                                      |
162    |   |                    |                            |                                                               |
163    |   |                    |                            | #.  Upper limit enforced per Traffic Class (TC) at the        |
164    |   |                    |                            |     subport level.                                            |
165    |   |                    |                            |                                                               |
166    |   |                    |                            | #.  Lower priority TCs able to reuse subport bandwidth        |
167    |   |                    |                            |     currently unused by higher priority TCs.                  |
168    |   |                    |                            |                                                               |
169    +---+--------------------+----------------------------+---------------------------------------------------------------+
170    | 3 | Pipe               | Configurable (default: 4K) | #.  Traffic shaping using the token bucket algorithm (one     |
171    |   |                    |                            |     token bucket per pipe.                                    |
172    |   |                    |                            |                                                               |
173    +---+--------------------+----------------------------+---------------------------------------------------------------+
174    | 4 | Traffic Class (TC) | 4                          | #.  TCs of the same pipe handled in strict priority order.    |
175    |   |                    |                            |                                                               |
176    |   |                    |                            | #.  Upper limit enforced per TC at the pipe level.            |
177    |   |                    |                            |                                                               |
178    |   |                    |                            | #.  Lower priority TCs able to reuse pipe bandwidth currently |
179    |   |                    |                            |     unused by higher priority TCs.                            |
180    |   |                    |                            |                                                               |
181    |   |                    |                            | #.  When subport TC is oversubscribed (configuration time     |
182    |   |                    |                            |     event), pipe TC upper limit is capped to a dynamically    |
183    |   |                    |                            |     adjusted value that is shared by all the subport pipes.   |
184    |   |                    |                            |                                                               |
185    +---+--------------------+----------------------------+---------------------------------------------------------------+
186    | 5 | Queue              | 4                          | #.  Queues of the same TC are serviced using Weighted Round   |
187    |   |                    |                            |     Robin (WRR) according to predefined weights.              |
188    |   |                    |                            |                                                               |
189    +---+--------------------+----------------------------+---------------------------------------------------------------+
190
191 Application Programming Interface (API)
192 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
193
194 Port Scheduler Configuration API
195 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
196
197 The rte_sched.h file contains configuration functions for port, subport and pipe.
198
199 Port Scheduler Enqueue API
200 ^^^^^^^^^^^^^^^^^^^^^^^^^^
201
202 The port scheduler enqueue API is very similar to the API of the DPDK PMD TX function.
203
204 .. code-block:: c
205
206     int rte_sched_port_enqueue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts);
207
208 Port Scheduler Dequeue API
209 ^^^^^^^^^^^^^^^^^^^^^^^^^^
210
211 The port scheduler dequeue API is very similar to the API of the DPDK PMD RX function.
212
213 .. code-block:: c
214
215     int rte_sched_port_dequeue(struct rte_sched_port *port, struct rte_mbuf **pkts, uint32_t n_pkts);
216
217 Usage Example
218 ^^^^^^^^^^^^^
219
220 .. code-block:: c
221
222     /* File "application.c" */
223
224     #define N_PKTS_RX   64
225     #define N_PKTS_TX   48
226     #define NIC_RX_PORT 0
227     #define NIC_RX_QUEUE 0
228     #define NIC_TX_PORT 1
229     #define NIC_TX_QUEUE 0
230
231     struct rte_sched_port *port = NULL;
232     struct rte_mbuf *pkts_rx[N_PKTS_RX], *pkts_tx[N_PKTS_TX];
233     uint32_t n_pkts_rx, n_pkts_tx;
234
235     /* Initialization */
236
237     <initialization code>
238
239     /* Runtime */
240     while (1) {
241         /* Read packets from NIC RX queue */
242
243         n_pkts_rx = rte_eth_rx_burst(NIC_RX_PORT, NIC_RX_QUEUE, pkts_rx, N_PKTS_RX);
244
245         /* Hierarchical scheduler enqueue */
246
247         rte_sched_port_enqueue(port, pkts_rx, n_pkts_rx);
248
249         /* Hierarchical scheduler dequeue */
250
251         n_pkts_tx = rte_sched_port_dequeue(port, pkts_tx, N_PKTS_TX);
252
253         /* Write packets to NIC TX queue */
254
255         rte_eth_tx_burst(NIC_TX_PORT, NIC_TX_QUEUE, pkts_tx, n_pkts_tx);
256     }
257
258 Implementation
259 ~~~~~~~~~~~~~~
260
261 Internal Data Structures per Port
262 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
263
264 A schematic of the internal data structures in shown in with details in.
265
266 .. _figure_data_struct_per_port:
267
268 .. figure:: img/data_struct_per_port.*
269
270     Internal Data Structures per Port
271
272
273 .. _table_qos_4:
274
275 .. table:: Scheduler Internal Data Structures per Port
276
277    +---+----------------------+-------------------------+---------------------+------------------------------+---------------------------------------------------+
278    | # | Data structure       | Size (bytes)            | # per port          | Access type                  | Description                                       |
279    |   |                      |                         |                     |                              |                                                   |
280    |   |                      |                         |                     +-------------+----------------+---------------------------------------------------+
281    |   |                      |                         |                     | Enq         | Deq            |                                                   |
282    |   |                      |                         |                     |             |                |                                                   |
283    +===+======================+=========================+=====================+=============+================+===================================================+
284    | 1 | Subport table entry  | 64                      | # subports per port | -           | Rd, Wr         | Persistent subport data (credits, etc).           |
285    |   |                      |                         |                     |             |                |                                                   |
286    +---+----------------------+-------------------------+---------------------+-------------+----------------+---------------------------------------------------+
287    | 2 | Pipe table entry     | 64                      | # pipes per port    | -           | Rd, Wr         | Persistent data for pipe, its TCs and its queues  |
288    |   |                      |                         |                     |             |                | (credits, etc) that is updated during run-time.   |
289    |   |                      |                         |                     |             |                |                                                   |
290    |   |                      |                         |                     |             |                | The pipe configuration parameters do not change   |
291    |   |                      |                         |                     |             |                | during run-time. The same pipe configuration      |
292    |   |                      |                         |                     |             |                | parameters are shared by multiple pipes,          |
293    |   |                      |                         |                     |             |                | therefore they are not part of pipe table entry.  |
294    |   |                      |                         |                     |             |                |                                                   |
295    +---+----------------------+-------------------------+---------------------+-------------+----------------+---------------------------------------------------+
296    | 3 | Queue table entry    | 4                       | #queues per port    | Rd, Wr      | Rd, Wr         | Persistent queue data (read and write pointers).  |
297    |   |                      |                         |                     |             |                | The queue size is the same per TC for all queues, |
298    |   |                      |                         |                     |             |                | allowing the queue base address to be computed    |
299    |   |                      |                         |                     |             |                | using a fast formula, so these two parameters are |
300    |   |                      |                         |                     |             |                | not part of queue table entry.                    |
301    |   |                      |                         |                     |             |                |                                                   |
302    |   |                      |                         |                     |             |                | The queue table entries for any given pipe are    |
303    |   |                      |                         |                     |             |                | stored in the same cache line.                    |
304    |   |                      |                         |                     |             |                |                                                   |
305    +---+----------------------+-------------------------+---------------------+-------------+----------------+---------------------------------------------------+
306    | 4 | Queue storage area   | Config (default: 64 x8) | # queues per port   | Wr          | Rd             | Array of elements per queue; each element is 8    |
307    |   |                      |                         |                     |             |                | byte in size (mbuf pointer).                      |
308    |   |                      |                         |                     |             |                |                                                   |
309    +---+----------------------+-------------------------+---------------------+-------------+----------------+---------------------------------------------------+
310    | 5 | Active queues bitmap | 1 bit per queue         | 1                   | Wr (Set)    | Rd, Wr (Clear) | The bitmap maintains one status bit per queue:    |
311    |   |                      |                         |                     |             |                | queue not active (queue is empty) or queue active |
312    |   |                      |                         |                     |             |                | (queue is not empty).                             |
313    |   |                      |                         |                     |             |                |                                                   |
314    |   |                      |                         |                     |             |                | Queue bit is set by the scheduler enqueue and     |
315    |   |                      |                         |                     |             |                | cleared by the scheduler dequeue when queue       |
316    |   |                      |                         |                     |             |                | becomes empty.                                    |
317    |   |                      |                         |                     |             |                |                                                   |
318    |   |                      |                         |                     |             |                | Bitmap scan operation returns the next non-empty  |
319    |   |                      |                         |                     |             |                | pipe and its status (16-bit mask of active queue  |
320    |   |                      |                         |                     |             |                | in the pipe).                                     |
321    |   |                      |                         |                     |             |                |                                                   |
322    +---+----------------------+-------------------------+---------------------+-------------+----------------+---------------------------------------------------+
323    | 6 | Grinder              | ~128                    | Config (default: 8) | -           | Rd, Wr         | Short list of active pipes currently under        |
324    |   |                      |                         |                     |             |                | processing. The grinder contains temporary data   |
325    |   |                      |                         |                     |             |                | during pipe processing.                           |
326    |   |                      |                         |                     |             |                |                                                   |
327    |   |                      |                         |                     |             |                | Once the current pipe exhausts packets or         |
328    |   |                      |                         |                     |             |                | credits, it is replaced with another active pipe  |
329    |   |                      |                         |                     |             |                | from the bitmap.                                  |
330    |   |                      |                         |                     |             |                |                                                   |
331    +---+----------------------+-------------------------+---------------------+-------------+----------------+---------------------------------------------------+
332
333 Multicore Scaling Strategy
334 ^^^^^^^^^^^^^^^^^^^^^^^^^^
335
336 The multicore scaling strategy is:
337
338 #.  Running different physical ports on different threads. The enqueue and dequeue of the same port are run by the same thread.
339
340 #.  Splitting the same physical port to different threads by running different sets of subports of the same physical port (virtual ports) on different threads.
341     Similarly, a subport can be split into multiple subports that are each run by a different thread.
342     The enqueue and dequeue of the same port are run by the same thread.
343     This is only required if, for performance reasons, it is not possible to handle a full port with a single core.
344
345 Enqueue and Dequeue for the Same Output Port
346 """"""""""""""""""""""""""""""""""""""""""""
347
348 Running enqueue and dequeue operations for the same output port from different cores is likely to cause significant impact on scheduler's performance
349 and it is therefore not recommended.
350
351 The port enqueue and dequeue operations share access to the following data structures:
352
353 #.  Packet descriptors
354
355 #.  Queue table
356
357 #.  Queue storage area
358
359 #.  Bitmap of active queues
360
361 The expected drop in performance is due to:
362
363 #.  Need to make the queue and bitmap operations thread safe,
364     which requires either using locking primitives for access serialization (for example, spinlocks/ semaphores) or
365     using atomic primitives for lockless access (for example, Test and Set, Compare And Swap, an so on).
366     The impact is much higher in the former case.
367
368 #.  Ping-pong of cache lines storing the shared data structures between the cache hierarchies of the two cores
369     (done transparently by the MESI protocol cache coherency CPU hardware).
370
371 Therefore, the scheduler enqueue and dequeue operations have to be run from the same thread,
372 which allows the queues and the bitmap operations to be non-thread safe and
373 keeps the scheduler data structures internal to the same core.
374
375 Performance Scaling
376 """""""""""""""""""
377
378 Scaling up the number of NIC ports simply requires a proportional increase in the number of CPU cores to be used for traffic scheduling.
379
380 Enqueue Pipeline
381 ^^^^^^^^^^^^^^^^
382
383 The sequence of steps per packet:
384
385 #.  *Access* the mbuf to read the data fields required to identify the destination queue for the packet.
386     These fields are: port, subport, traffic class and queue within traffic class, and are typically set by the classification stage.
387
388 #.  *Access* the queue structure to identify the write location in the queue array.
389     If the queue is full, then the packet is discarded.
390
391 #.  *Access* the queue array location to store the packet (i.e. write the mbuf pointer).
392
393 It should be noted the strong data dependency between these steps, as steps 2 and 3 cannot start before the result from steps 1 and 2 becomes available,
394 which prevents the processor out of order execution engine to provide any significant performance optimizations.
395
396 Given the high rate of input packets and the large amount of queues,
397 it is expected that the data structures accessed to enqueue the current packet are not present
398 in the L1 or L2 data cache of the current core, thus the above 3 memory accesses would result (on average) in L1 and L2 data cache misses.
399 A number of 3 L1/L2 cache misses per packet is not acceptable for performance reasons.
400
401 The workaround is to prefetch the required data structures in advance. The prefetch operation has an execution latency during which
402 the processor should not attempt to access the data structure currently under prefetch, so the processor should execute other work.
403 The only other work available is to execute different stages of the enqueue sequence of operations on other input packets,
404 thus resulting in a pipelined implementation for the enqueue operation.
405
406 :numref:`figure_prefetch_pipeline` illustrates a pipelined implementation for the enqueue operation with 4 pipeline stages and each stage executing 2 different input packets.
407 No input packet can be part of more than one pipeline stage at a given time.
408
409 .. _figure_prefetch_pipeline:
410
411 .. figure:: img/prefetch_pipeline.*
412
413     Prefetch Pipeline for the Hierarchical Scheduler Enqueue Operation
414
415
416 The congestion management scheme implemented by the enqueue pipeline described above is very basic:
417 packets are enqueued until a specific queue becomes full,
418 then all the packets destined to the same queue are dropped until packets are consumed (by the dequeue operation).
419 This can be improved by enabling RED/WRED as part of the enqueue pipeline which looks at the queue occupancy and
420 packet priority in order to yield the enqueue/drop decision for a specific packet
421 (as opposed to enqueuing all packets / dropping all packets indiscriminately).
422
423 Dequeue State Machine
424 ^^^^^^^^^^^^^^^^^^^^^
425
426 The sequence of steps to schedule the next packet from the current pipe is:
427
428 #.  Identify the next active pipe using the bitmap scan operation, *prefetch* pipe.
429
430 #.  *Read* pipe data structure. Update the credits for the current pipe and its subport.
431     Identify the first active traffic class within the current pipe, select the next queue using WRR,
432     *prefetch* queue pointers for all the 16 queues of the current pipe.
433
434 #.  *Read* next element from the current WRR queue and *prefetch* its packet descriptor.
435
436 #.  *Read* the packet length from the packet descriptor (mbuf structure).
437     Based on the packet length and the available credits (of current pipe, pipe traffic class, subport and subport traffic class),
438     take the go/no go scheduling decision for the current packet.
439
440 To avoid the cache misses, the above data structures (pipe, queue, queue array, mbufs) are prefetched in advance of being accessed.
441 The strategy of hiding the latency of the prefetch operations is to switch from the current pipe (in grinder A) to another pipe
442 (in grinder B) immediately after a prefetch is issued for the current pipe.
443 This gives enough time to the prefetch operation to complete before the execution switches back to this pipe (in grinder A).
444
445 The dequeue pipe state machine exploits the data presence into the processor cache,
446 therefore it tries to send as many packets from the same pipe TC and pipe as possible (up to the available packets and credits) before
447 moving to the next active TC from the same pipe (if any) or to another active pipe.
448
449 .. _figure_pipe_prefetch_sm:
450
451 .. figure:: img/pipe_prefetch_sm.*
452
453    Pipe Prefetch State Machine for the Hierarchical Scheduler Dequeue
454    Operation
455
456
457 Timing and Synchronization
458 ^^^^^^^^^^^^^^^^^^^^^^^^^^
459
460 The output port is modeled as a conveyor belt of byte slots that need to be filled by the scheduler with data for transmission.
461 For 10 GbE, there are 1.25 billion byte slots that need to be filled by the port scheduler every second.
462 If the scheduler is not fast enough to fill the slots, provided that enough packets and credits exist,
463 then some slots will be left unused and bandwidth will be wasted.
464
465 In principle, the hierarchical scheduler dequeue operation should be triggered by NIC TX.
466 Usually, once the occupancy of the NIC TX input queue drops below a predefined threshold,
467 the port scheduler is woken up (interrupt based or polling based,
468 by continuously monitoring the queue occupancy) to push more packets into the queue.
469
470 Internal Time Reference
471 """""""""""""""""""""""
472
473 The scheduler needs to keep track of time advancement for the credit logic,
474 which requires credit updates based on time (for example, subport and pipe traffic shaping, traffic class upper limit enforcement, and so on).
475
476 Every time the scheduler decides to send a packet out to the NIC TX for transmission, the scheduler will increment its internal time reference accordingly.
477 Therefore, it is convenient to keep the internal time reference in units of bytes,
478 where a byte signifies the time duration required by the physical interface to send out a byte on the transmission medium.
479 This way, as a packet is scheduled for transmission, the time is incremented with (n + h),
480 where n is the packet length in bytes and h is the number of framing overhead bytes per packet.
481
482 Internal Time Reference Re-synchronization
483 """"""""""""""""""""""""""""""""""""""""""
484
485 The scheduler needs to align its internal time reference to the pace of the port conveyor belt.
486 The reason is to make sure that the scheduler does not feed the NIC TX with more bytes than the line rate of the physical medium in order to prevent packet drop
487 (by the scheduler, due to the NIC TX input queue being full, or later on, internally by the NIC TX).
488
489 The scheduler reads the current time on every dequeue invocation.
490 The CPU time stamp can be obtained by reading either the Time Stamp Counter (TSC) register or the High Precision Event Timer (HPET) register.
491 The current CPU time stamp is converted from number of CPU clocks to number of bytes:
492 *time_bytes = time_cycles / cycles_per_byte, where cycles_per_byte*
493 is the amount of CPU cycles that is equivalent to the transmission time for one byte on the wire
494 (e.g. for a CPU frequency of 2 GHz and a 10GbE port,*cycles_per_byte = 1.6*).
495
496 The scheduler maintains an internal time reference of the NIC time.
497 Whenever a packet is scheduled, the NIC time is incremented with the packet length (including framing overhead).
498 On every dequeue invocation, the scheduler checks its internal reference of the NIC time against the current time:
499
500 #. If NIC time is in the future (NIC time >= current time), no adjustment of NIC time is needed.
501    This means that scheduler is able to schedule NIC packets before the NIC actually needs those packets, so the NIC TX is well supplied with packets;
502
503 #. If NIC time is in the past (NIC time < current time), then NIC time should be adjusted by setting it to the current time.
504    This means that the scheduler is not able to keep up with the speed of the NIC byte conveyor belt,
505    so NIC bandwidth is wasted due to poor packet supply to the NIC TX.
506
507 Scheduler Accuracy and Granularity
508 """"""""""""""""""""""""""""""""""
509
510 The scheduler round trip delay (SRTD) is the time (number of CPU cycles) between two consecutive examinations of the same pipe by the scheduler.
511
512 To keep up with the output port (that is, avoid bandwidth loss),
513 the scheduler should be able to schedule n packets faster than the same n packets are transmitted by NIC TX.
514
515 The scheduler needs to keep up with the rate of each individual pipe,
516 as configured for the pipe token bucket, assuming that no port oversubscription is taking place.
517 This means that the size of the pipe token bucket should be set high enough to prevent it from overflowing due to big SRTD,
518 as this would result in credit loss (and therefore bandwidth loss) for the pipe.
519
520 Credit Logic
521 ^^^^^^^^^^^^
522
523 Scheduling Decision
524 """""""""""""""""""
525
526 The scheduling decision to send next packet from (subport S, pipe P, traffic class TC, queue Q) is favorable (packet is sent)
527 when all the conditions below are met:
528
529 *   Pipe P of subport S is currently selected by one of the port grinders;
530
531 *   Traffic class TC is the highest priority active traffic class of pipe P;
532
533 *   Queue Q is the next queue selected by WRR within traffic class TC of pipe P;
534
535 *   Subport S has enough credits to send the packet;
536
537 *   Subport S has enough credits for traffic class TC to send the packet;
538
539 *   Pipe P has enough credits to send the packet;
540
541 *   Pipe P has enough credits for traffic class TC to send the packet.
542
543 If all the above conditions are met,
544 then the packet is selected for transmission and the necessary credits are subtracted from subport S,
545 subport S traffic class TC, pipe P, pipe P traffic class TC.
546
547 Framing Overhead
548 """"""""""""""""
549
550 As the greatest common divisor for all packet lengths is one byte, the unit of credit is selected as one byte.
551 The number of credits required for the transmission of a packet of n bytes is equal to (n+h),
552 where h is equal to the number of framing overhead bytes per packet.
553
554 .. _table_qos_5:
555
556 .. table:: Ethernet Frame Overhead Fields
557
558    +---+--------------------------------+----------------+---------------------------------------------------------------------------+
559    | # | Packet field                   | Length (bytes) | Comments                                                                  |
560    |   |                                |                |                                                                           |
561    +===+================================+================+===========================================================================+
562    | 1 | Preamble                       | 7              |                                                                           |
563    |   |                                |                |                                                                           |
564    +---+--------------------------------+----------------+---------------------------------------------------------------------------+
565    | 2 | Start of Frame Delimiter (SFD) | 1              |                                                                           |
566    |   |                                |                |                                                                           |
567    +---+--------------------------------+----------------+---------------------------------------------------------------------------+
568    | 3 | Frame Check Sequence (FCS)     | 4              | Considered overhead only if not included in the mbuf packet length field. |
569    |   |                                |                |                                                                           |
570    +---+--------------------------------+----------------+---------------------------------------------------------------------------+
571    | 4 | Inter Frame Gap (IFG)          | 12             |                                                                           |
572    |   |                                |                |                                                                           |
573    +---+--------------------------------+----------------+---------------------------------------------------------------------------+
574    | 5 | Total                          | 24             |                                                                           |
575    |   |                                |                |                                                                           |
576    +---+--------------------------------+----------------+---------------------------------------------------------------------------+
577
578 Traffic Shaping
579 """""""""""""""
580
581 The traffic shaping for subport and pipe is implemented using a token bucket per subport/per pipe.
582 Each token bucket is implemented using one saturated counter that keeps track of the number of available credits.
583
584 The token bucket generic parameters and operations are presented in :numref:`table_qos_6` and :numref:`table_qos_7`.
585
586 .. _table_qos_6:
587
588 .. table:: Token Bucket Generic Parameters
589
590    +---+------------------------+--------------------+---------------------------------------------------------+
591    | # | Token Bucket Parameter | Unit               | Description                                             |
592    |   |                        |                    |                                                         |
593    +===+========================+====================+=========================================================+
594    | 1 | bucket_rate            | Credits per second | Rate of adding credits to the bucket.                   |
595    |   |                        |                    |                                                         |
596    +---+------------------------+--------------------+---------------------------------------------------------+
597    | 2 | bucket_size            | Credits            | Max number of credits that can be stored in the bucket. |
598    |   |                        |                    |                                                         |
599    +---+------------------------+--------------------+---------------------------------------------------------+
600
601 .. _table_qos_7:
602
603 .. table:: Token Bucket Generic Operations
604
605    +---+------------------------+------------------------------------------------------------------------------+
606    | # | Token Bucket Operation | Description                                                                  |
607    |   |                        |                                                                              |
608    +===+========================+==============================================================================+
609    | 1 | Initialization         | Bucket set to a predefined value, e.g. zero or half of the bucket size.      |
610    |   |                        |                                                                              |
611    +---+------------------------+------------------------------------------------------------------------------+
612    | 2 | Credit update          | Credits are added to the bucket on top of existing ones, either periodically |
613    |   |                        | or on demand, based on the bucket_rate. Credits cannot exceed the upper      |
614    |   |                        | limit defined by the bucket_size, so any credits to be added to the bucket   |
615    |   |                        | while the bucket is full are dropped.                                        |
616    |   |                        |                                                                              |
617    +---+------------------------+------------------------------------------------------------------------------+
618    | 3 | Credit consumption     | As result of packet scheduling, the necessary number of credits is removed   |
619    |   |                        | from the bucket. The packet can only be sent if enough credits are in the    |
620    |   |                        | bucket to send the full packet (packet bytes and framing overhead for the    |
621    |   |                        | packet).                                                                     |
622    |   |                        |                                                                              |
623    +---+------------------------+------------------------------------------------------------------------------+
624
625 To implement the token bucket generic operations described above,
626 the current design uses the persistent data structure presented in :numref:`table_qos_8`,
627 while the implementation of the token bucket operations is described in :numref:`table_qos_9`.
628
629 .. _table_qos_8:
630
631 .. table:: Token Bucket Persistent Data Structure
632
633    +---+------------------------+-------+----------------------------------------------------------------------+
634    | # | Token bucket field     | Unit  | Description                                                          |
635    |   |                        |       |                                                                      |
636    +===+========================+=======+======================================================================+
637    | 1 | tb_time                | Bytes | Time of the last credit update. Measured in bytes instead of seconds |
638    |   |                        |       | or CPU cycles for ease of credit consumption operation               |
639    |   |                        |       | (as the current time is also maintained in bytes).                   |
640    |   |                        |       |                                                                      |
641    |   |                        |       | See  Section 26.2.4.5.1 "Internal Time Reference" for an             |
642    |   |                        |       | explanation of why the time is maintained in byte units.             |
643    |   |                        |       |                                                                      |
644    +---+------------------------+-------+----------------------------------------------------------------------+
645    | 2 | tb_period              | Bytes | Time period that should elapse since the last credit update in order |
646    |   |                        |       | for the bucket to be awarded tb_credits_per_period worth or credits. |
647    |   |                        |       |                                                                      |
648    +---+------------------------+-------+----------------------------------------------------------------------+
649    | 3 | tb_credits_per_period  | Bytes | Credit allowance per tb_period.                                      |
650    |   |                        |       |                                                                      |
651    +---+------------------------+-------+----------------------------------------------------------------------+
652    | 4 | tb_size                | Bytes | Bucket size, i.e. upper limit for the tb_credits.                    |
653    |   |                        |       |                                                                      |
654    +---+------------------------+-------+----------------------------------------------------------------------+
655    | 5 | tb_credits             | Bytes | Number of credits currently in the bucket.                           |
656    |   |                        |       |                                                                      |
657    +---+------------------------+-------+----------------------------------------------------------------------+
658
659 The bucket rate (in bytes per second) can be computed with the following formula:
660
661 *bucket_rate = (tb_credits_per_period / tb_period) * r*
662
663 where, r = port line rate (in bytes per second).
664
665 .. _table_qos_9:
666
667 .. table:: Token Bucket Operations
668
669    +---+-------------------------+-----------------------------------------------------------------------------+
670    | # | Token bucket operation  | Description                                                                 |
671    |   |                         |                                                                             |
672    +===+=========================+=============================================================================+
673    | 1 | Initialization          | *tb_credits = 0; or tb_credits = tb_size / 2;*                              |
674    |   |                         |                                                                             |
675    +---+-------------------------+-----------------------------------------------------------------------------+
676    | 2 | Credit update           | Credit update options:                                                      |
677    |   |                         |                                                                             |
678    |   |                         | *   Every time a packet is sent for a port, update the credits of all the   |
679    |   |                         |     the subports and pipes of that port. Not feasible.                      |
680    |   |                         |                                                                             |
681    |   |                         | *   Every time a packet is sent, update the credits for the pipe and        |
682    |   |                         |     subport. Very accurate, but not needed (a lot of calculations).         |
683    |   |                         |                                                                             |
684    |   |                         | *   Every time a pipe is selected (that is, picked by one                   |
685    |   |                         |     of the grinders), update the credits for the pipe and its subport.      |
686    |   |                         |                                                                             |
687    |   |                         | The current implementation is using option 3.  According to Section         |
688    |   |                         | `Dequeue State Machine`_, the pipe and subport credits are                  |
689    |   |                         | updated every time a pipe is selected by the dequeue process before the     |
690    |   |                         | pipe and subport credits are actually used.                                 |
691    |   |                         |                                                                             |
692    |   |                         | The implementation uses a tradeoff between accuracy and speed by updating   |
693    |   |                         | the bucket credits only when at least a full *tb_period*  has elapsed since |
694    |   |                         | the last update.                                                            |
695    |   |                         |                                                                             |
696    |   |                         | *   Full accuracy can be achieved by selecting the value for *tb_period*    |
697    |   |                         |     for which  *tb_credits_per_period = 1*.                                 |
698    |   |                         |                                                                             |
699    |   |                         | *   When full accuracy is not required, better performance is achieved by   |
700    |   |                         |     setting *tb_credits* to a larger value.                                 |
701    |   |                         |                                                                             |
702    |   |                         | Update operations:                                                          |
703    |   |                         |                                                                             |
704    |   |                         | *   n_periods = (time - tb_time) / tb_period;                               |
705    |   |                         |                                                                             |
706    |   |                         | *   tb_credits += n_periods * tb_credits_per_period;                        |
707    |   |                         |                                                                             |
708    |   |                         | *   tb_credits = min(tb_credits, tb_size);                                  |
709    |   |                         |                                                                             |
710    |   |                         | *   tb_time += n_periods * tb_period;                                       |
711    |   |                         |                                                                             |
712    +---+-------------------------+-----------------------------------------------------------------------------+
713    | 3 | Credit consumption      | As result of packet scheduling, the necessary number of credits is removed  |
714    |   |  (on packet scheduling) | from the bucket. The packet can only be sent if enough credits are in the   |
715    |   |                         | bucket to send the full packet (packet bytes and framing overhead for the   |
716    |   |                         | packet).                                                                    |
717    |   |                         |                                                                             |
718    |   |                         | Scheduling operations:                                                      |
719    |   |                         |                                                                             |
720    |   |                         | pkt_credits = pkt_len + frame_overhead;                                     |
721    |   |                         | if (tb_credits >= pkt_credits){tb_credits -= pkt_credits;}                  |
722    |   |                         |                                                                             |
723    +---+-------------------------+-----------------------------------------------------------------------------+
724
725 Traffic Classes
726 """""""""""""""
727
728 Implementation of Strict Priority Scheduling
729 ''''''''''''''''''''''''''''''''''''''''''''
730
731 Strict priority scheduling of traffic classes within the same pipe is implemented by the pipe dequeue state machine,
732 which selects the queues in ascending order.
733 Therefore, queues 0..3 (associated with TC 0, highest priority TC) are handled before
734 queues 4..7 (TC 1, lower priority than TC 0),
735 which are handled before queues 8..11 (TC 2),
736 which are handled before queues 12..15 (TC 3, lowest priority TC).
737
738 Upper Limit Enforcement
739 '''''''''''''''''''''''
740
741 The traffic classes at the pipe and subport levels are not traffic shaped,
742 so there is no token bucket maintained in this context.
743 The upper limit for the traffic classes at the subport and
744 pipe levels is enforced by periodically refilling the subport / pipe traffic class credit counter,
745 out of which credits are consumed every time a packet is scheduled for that subport / pipe,
746 as described in :numref:`table_qos_10` and :numref:`table_qos_11`.
747
748 .. _table_qos_10:
749
750 .. table:: Subport/Pipe Traffic Class Upper Limit Enforcement Persistent Data Structure
751
752    +---+-----------------------+-------+-----------------------------------------------------------------------+
753    | # | Subport or pipe field | Unit  | Description                                                           |
754    |   |                       |       |                                                                       |
755    +===+=======================+=======+=======================================================================+
756    | 1 | tc_time               | Bytes | Time of the next update (upper limit refill) for the 4 TCs of the     |
757    |   |                       |       | current subport / pipe.                                               |
758    |   |                       |       |                                                                       |
759    |   |                       |       | See  Section `Internal Time Reference`_ for the                       |
760    |   |                       |       | explanation of why the time is maintained in byte units.              |
761    |   |                       |       |                                                                       |
762    +---+-----------------------+-------+-----------------------------------------------------------------------+
763    | 2 | tc_period             | Bytes | Time between two consecutive updates for the 4 TCs of the current     |
764    |   |                       |       | subport / pipe. This is expected to be many times bigger than the     |
765    |   |                       |       | typical value of the token bucket tb_period.                          |
766    |   |                       |       |                                                                       |
767    +---+-----------------------+-------+-----------------------------------------------------------------------+
768    | 3 | tc_credits_per_period | Bytes | Upper limit for the number of credits allowed to be consumed by the   |
769    |   |                       |       | current TC during each enforcement period tc_period.                  |
770    |   |                       |       |                                                                       |
771    +---+-----------------------+-------+-----------------------------------------------------------------------+
772    | 4 | tc_credits            | Bytes | Current upper limit for the number of credits that can be consumed by |
773    |   |                       |       | the current traffic class for the remainder of the current            |
774    |   |                       |       | enforcement period.                                                   |
775    |   |                       |       |                                                                       |
776    +---+-----------------------+-------+-----------------------------------------------------------------------+
777
778 .. _table_qos_11:
779
780 .. table:: Subport/Pipe Traffic Class Upper Limit Enforcement Operations
781
782    +---+--------------------------+----------------------------------------------------------------------------+
783    | # | Traffic Class Operation  | Description                                                                |
784    |   |                          |                                                                            |
785    +===+==========================+============================================================================+
786    | 1 | Initialization           | tc_credits = tc_credits_per_period;                                        |
787    |   |                          |                                                                            |
788    |   |                          | tc_time = tc_period;                                                       |
789    |   |                          |                                                                            |
790    +---+--------------------------+----------------------------------------------------------------------------+
791    | 2 | Credit update            | Update operations:                                                         |
792    |   |                          |                                                                            |
793    |   |                          | if (time >= tc_time) {                                                     |
794    |   |                          |                                                                            |
795    |   |                          | tc_credits = tc_credits_per_period;                                        |
796    |   |                          |                                                                            |
797    |   |                          | tc_time = time + tc_period;                                                |
798    |   |                          |                                                                            |
799    |   |                          | }                                                                          |
800    |   |                          |                                                                            |
801    +---+--------------------------+----------------------------------------------------------------------------+
802    | 3 | Credit consumption       | As result of packet scheduling, the TC limit is decreased with the         |
803    |   | (on packet scheduling)   | necessary number of credits. The packet can only be sent if enough credits |
804    |   |                          | are currently available in the TC limit to send the full packet            |
805    |   |                          | (packet bytes and framing overhead for the packet).                        |
806    |   |                          |                                                                            |
807    |   |                          | Scheduling operations:                                                     |
808    |   |                          |                                                                            |
809    |   |                          | pkt_credits = pk_len + frame_overhead;                                     |
810    |   |                          |                                                                            |
811    |   |                          | if (tc_credits >= pkt_credits) {tc_credits -= pkt_credits;}                |
812    |   |                          |                                                                            |
813    +---+--------------------------+----------------------------------------------------------------------------+
814
815 Weighted Round Robin (WRR)
816 """"""""""""""""""""""""""
817
818 The evolution of the WRR design solution from simple to complex is shown in :numref:`table_qos_12`.
819
820 .. _table_qos_12:
821
822 .. table:: Weighted Round Robin (WRR)
823
824    +---+------------+-----------------+-------------+----------------------------------------------------------+
825    | # | All Queues | Equal Weights   | All Packets | Strategy                                                 |
826    |   | Active?    | for All Queues? | Equal?      |                                                          |
827    +===+============+=================+=============+==========================================================+
828    | 1 | Yes        | Yes             | Yes         | **Byte level round robin**                               |
829    |   |            |                 |             |                                                          |
830    |   |            |                 |             | *Next queue*  queue #i, i =  *(i + 1) % n*               |
831    |   |            |                 |             |                                                          |
832    +---+------------+-----------------+-------------+----------------------------------------------------------+
833    | 2 | Yes        | Yes             | No          | **Packet level round robin**                             |
834    |   |            |                 |             |                                                          |
835    |   |            |                 |             | Consuming one byte from queue #i requires consuming      |
836    |   |            |                 |             | exactly one token for queue #i.                          |
837    |   |            |                 |             |                                                          |
838    |   |            |                 |             | T(i) = Accumulated number of tokens previously consumed  |
839    |   |            |                 |             | from queue #i. Every time a packet is consumed from      |
840    |   |            |                 |             | queue #i, T(i) is updated as: T(i) += *pkt_len*.         |
841    |   |            |                 |             |                                                          |
842    |   |            |                 |             | *Next queue* : queue with the smallest T.                |
843    |   |            |                 |             |                                                          |
844    |   |            |                 |             |                                                          |
845    +---+------------+-----------------+-------------+----------------------------------------------------------+
846    | 3 | Yes        | No              | No          | **Packet level weighted round robin**                    |
847    |   |            |                 |             |                                                          |
848    |   |            |                 |             | This case can be reduced to the previous case by         |
849    |   |            |                 |             | introducing a cost per byte that is different for each   |
850    |   |            |                 |             | queue. Queues with lower weights have a higher cost per  |
851    |   |            |                 |             | byte. This way, it is still meaningful to compare the    |
852    |   |            |                 |             | consumption amongst different queues in order to select  |
853    |   |            |                 |             | the next queue.                                          |
854    |   |            |                 |             |                                                          |
855    |   |            |                 |             | w(i) = Weight of queue #i                                |
856    |   |            |                 |             |                                                          |
857    |   |            |                 |             | t(i) = Tokens per byte for queue #i, defined as the      |
858    |   |            |                 |             | inverse weight of queue #i.                              |
859    |   |            |                 |             | For example, if w[0..3] = [1:2:4:8],                     |
860    |   |            |                 |             | then t[0..3] = [8:4:2:1]; if w[0..3] = [1:4:15:20],      |
861    |   |            |                 |             | then t[0..3] = [60:15:4:3].                              |
862    |   |            |                 |             | Consuming one byte from queue #i requires consuming t(i) |
863    |   |            |                 |             | tokens for queue #i.                                     |
864    |   |            |                 |             |                                                          |
865    |   |            |                 |             | T(i) = Accumulated number of tokens previously consumed  |
866    |   |            |                 |             | from queue #i. Every time a packet is consumed from      |
867    |   |            |                 |             | queue #i, T(i) is updated as:  *T(i) += pkt_len * t(i)*. |
868    |   |            |                 |             | *Next queue* : queue with the smallest T.                |
869    |   |            |                 |             |                                                          |
870    +---+------------+-----------------+-------------+----------------------------------------------------------+
871    | 4 | No         | No              | No          | **Packet level weighted round robin with variable queue  |
872    |   |            |                 |             | status**                                                 |
873    |   |            |                 |             |                                                          |
874    |   |            |                 |             | Reduce this case to the previous case by setting the     |
875    |   |            |                 |             | consumption of inactive queues to a high number, so that |
876    |   |            |                 |             | the inactive queues will never be selected by the        |
877    |   |            |                 |             | smallest T logic.                                        |
878    |   |            |                 |             |                                                          |
879    |   |            |                 |             | To prevent T from overflowing as result of successive    |
880    |   |            |                 |             | accumulations, T(i) is truncated after each packet       |
881    |   |            |                 |             | consumption for all queues.                              |
882    |   |            |                 |             | For example, T[0..3] = [1000, 1100, 1200, 1300]          |
883    |   |            |                 |             | is truncated to T[0..3] = [0, 100, 200, 300]             |
884    |   |            |                 |             | by subtracting the min T from T(i), i = 0..n.            |
885    |   |            |                 |             |                                                          |
886    |   |            |                 |             | This requires having at least one active queue in the    |
887    |   |            |                 |             | set of input queues, which is guaranteed by the dequeue  |
888    |   |            |                 |             | state machine never selecting an inactive traffic class. |
889    |   |            |                 |             |                                                          |
890    |   |            |                 |             | *mask(i) = Saturation mask for queue #i, defined as:*    |
891    |   |            |                 |             |                                                          |
892    |   |            |                 |             | mask(i) = (queue #i is active)? 0 : 0xFFFFFFFF;          |
893    |   |            |                 |             |                                                          |
894    |   |            |                 |             | w(i) = Weight of queue #i                                |
895    |   |            |                 |             |                                                          |
896    |   |            |                 |             | t(i) = Tokens per byte for queue #i, defined as the      |
897    |   |            |                 |             | inverse weight of queue #i.                              |
898    |   |            |                 |             |                                                          |
899    |   |            |                 |             | T(i) = Accumulated numbers of tokens previously consumed |
900    |   |            |                 |             | from queue #i.                                           |
901    |   |            |                 |             |                                                          |
902    |   |            |                 |             | *Next queue*  : queue with smallest T.                   |
903    |   |            |                 |             |                                                          |
904    |   |            |                 |             | Before packet consumption from queue #i:                 |
905    |   |            |                 |             |                                                          |
906    |   |            |                 |             | *T(i) |= mask(i)*                                        |
907    |   |            |                 |             |                                                          |
908    |   |            |                 |             | After packet consumption from queue #i:                  |
909    |   |            |                 |             |                                                          |
910    |   |            |                 |             | T(j) -= T(i), j != i                                     |
911    |   |            |                 |             |                                                          |
912    |   |            |                 |             | T(i) = pkt_len * t(i)                                    |
913    |   |            |                 |             |                                                          |
914    |   |            |                 |             | Note: T(j) uses the T(i) value before T(i) is updated.   |
915    |   |            |                 |             |                                                          |
916    +---+------------+-----------------+-------------+----------------------------------------------------------+
917
918 Subport Traffic Class Oversubscription
919 """"""""""""""""""""""""""""""""""""""
920
921 Problem Statement
922 '''''''''''''''''
923
924 Oversubscription for subport traffic class X is a configuration-time event that occurs when
925 more bandwidth is allocated for traffic class X at the level of subport member pipes than
926 allocated for the same traffic class at the parent subport level.
927
928 The existence of the oversubscription for a specific subport and
929 traffic class is solely the result of pipe and
930 subport-level configuration as opposed to being created due
931 to dynamic evolution of the traffic load at run-time (as congestion is).
932
933 When the overall demand for traffic class X for the current subport is low,
934 the existence of the oversubscription condition does not represent a problem,
935 as demand for traffic class X is completely satisfied for all member pipes.
936 However, this can no longer be achieved when the aggregated demand for traffic class X
937 for all subport member pipes exceeds the limit configured at the subport level.
938
939 Solution Space
940 ''''''''''''''
941
942 summarizes some of the possible approaches for handling this problem,
943 with the third approach selected for implementation.
944
945 .. _table_qos_13:
946
947 .. table:: Subport Traffic Class Oversubscription
948
949    +-----+---------------------------+-------------------------------------------------------------------------+
950    | No. | Approach                  | Description                                                             |
951    |     |                           |                                                                         |
952    +=====+===========================+=========================================================================+
953    | 1   | Don't care                | First come, first served.                                               |
954    |     |                           |                                                                         |
955    |     |                           | This approach is not fair amongst subport member pipes, as pipes that   |
956    |     |                           | are served first will use up as much bandwidth for TC X as they need,   |
957    |     |                           | while pipes that are served later will receive poor service due to      |
958    |     |                           | bandwidth for TC X at the subport level being scarce.                   |
959    |     |                           |                                                                         |
960    +-----+---------------------------+-------------------------------------------------------------------------+
961    | 2   | Scale down all pipes      | All pipes within the subport have their bandwidth limit for TC X scaled |
962    |     |                           | down by the same factor.                                                |
963    |     |                           |                                                                         |
964    |     |                           | This approach is not fair among subport member pipes, as the low end    |
965    |     |                           | pipes (that is, pipes configured with low bandwidth) can potentially    |
966    |     |                           | experience severe service degradation that might render their service   |
967    |     |                           | unusable (if available bandwidth for these pipes drops below the        |
968    |     |                           | minimum requirements for a workable service), while the service         |
969    |     |                           | degradation for high end pipes might not be noticeable at all.          |
970    |     |                           |                                                                         |
971    +-----+---------------------------+-------------------------------------------------------------------------+
972    | 3   | Cap the high demand pipes | Each subport member pipe receives an equal share of the bandwidth       |
973    |     |                           | available at run-time for TC X at the subport level. Any bandwidth left |
974    |     |                           | unused by the low-demand pipes is redistributed in equal portions to    |
975    |     |                           | the high-demand pipes. This way, the high-demand pipes are truncated    |
976    |     |                           | while the low-demand pipes are not impacted.                            |
977    |     |                           |                                                                         |
978    +-----+---------------------------+-------------------------------------------------------------------------+
979
980 Typically, the subport TC oversubscription feature is enabled only for the lowest priority traffic class (TC 3),
981 which is typically used for best effort traffic,
982 with the management plane preventing this condition from occurring for the other (higher priority) traffic classes.
983
984 To ease implementation, it is also assumed that the upper limit for subport TC 3 is set to 100% of the subport rate,
985 and that the upper limit for pipe TC 3 is set to 100% of pipe rate for all subport member pipes.
986
987 Implementation Overview
988 '''''''''''''''''''''''
989
990 The algorithm computes a watermark, which is periodically updated based on the current demand experienced by the subport member pipes,
991 whose purpose is to limit the amount of traffic that each pipe is allowed to send for TC 3.
992 The watermark is computed at the subport level at the beginning of each traffic class upper limit enforcement period and
993 the same value is used by all the subport member pipes throughout the current enforcement period.
994 illustrates how the watermark computed as subport level at the beginning of each period is propagated to all subport member pipes.
995
996 At the beginning of the current enforcement period (which coincides with the end of the previous enforcement period),
997 the value of the watermark is adjusted based on the amount of bandwidth allocated to TC 3 at the beginning of the previous period that
998 was not left unused by the subport member pipes at the end of the previous period.
999
1000 If there was subport TC 3 bandwidth left unused,
1001 the value of the watermark for the current period is increased to encourage the subport member pipes to consume more bandwidth.
1002 Otherwise, the value of the watermark is decreased to enforce equality of bandwidth consumption among subport member pipes for TC 3.
1003
1004 The increase or decrease in the watermark value is done in small increments,
1005 so several enforcement periods might be required to reach the equilibrium state.
1006 This state can change at any moment due to variations in the demand experienced by the subport member pipes for TC 3, for example,
1007 as a result of demand increase (when the watermark needs to be lowered) or demand decrease (when the watermark needs to be increased).
1008
1009 When demand is low, the watermark is set high to prevent it from impeding the subport member pipes from consuming more bandwidth.
1010 The highest value for the watermark is picked as the highest rate configured for a subport member pipe.
1011 :numref:`table_qos_14` and :numref:`table_qos_15` illustrates the watermark operation.
1012
1013 .. _table_qos_14:
1014
1015 .. table:: Watermark Propagation from Subport Level to Member Pipes at the Beginning of Each Traffic Class Upper Limit Enforcement Period
1016
1017    +-----+---------------------------------+----------------------------------------------------+
1018    | No. | Subport Traffic Class Operation | Description                                        |
1019    |     |                                 |                                                    |
1020    +=====+=================================+====================================================+
1021    | 1   | Initialization                  | **Subport level**: subport_period_id= 0            |
1022    |     |                                 |                                                    |
1023    |     |                                 | **Pipe level**: pipe_period_id = 0                 |
1024    |     |                                 |                                                    |
1025    +-----+---------------------------------+----------------------------------------------------+
1026    | 2   | Credit update                   | **Subport Level**:                                 |
1027    |     |                                 |                                                    |
1028    |     |                                 | if (time>=subport_tc_time)                         |
1029    |     |                                 |                                                    |
1030    |     |                                 | {                                                  |
1031    |     |                                 |     subport_wm = water_mark_update();              |
1032    |     |                                 |                                                    |
1033    |     |                                 |     subport_tc_time = time + subport_tc_period;    |
1034    |     |                                 |                                                    |
1035    |     |                                 |     subport_period_id++;                           |
1036    |     |                                 |                                                    |
1037    |     |                                 | }                                                  |
1038    |     |                                 |                                                    |
1039    |     |                                 | **Pipelevel:**                                     |
1040    |     |                                 |                                                    |
1041    |     |                                 | if(pipe_period_id != subport_period_id)            |
1042    |     |                                 |                                                    |
1043    |     |                                 | {                                                  |
1044    |     |                                 |                                                    |
1045    |     |                                 |     pipe_ov_credits = subport_wm \* pipe_weight;   |
1046    |     |                                 |                                                    |
1047    |     |                                 |     pipe_period_id = subport_period_id;            |
1048    |     |                                 |                                                    |
1049    |     |                                 | }                                                  |
1050    |     |                                 |                                                    |
1051    +-----+---------------------------------+----------------------------------------------------+
1052    | 3   | Credit consumption              | **Pipe level:**                                    |
1053    |     | (on packet scheduling)          |                                                    |
1054    |     |                                 | pkt_credits = pk_len + frame_overhead;             |
1055    |     |                                 |                                                    |
1056    |     |                                 | if(pipe_ov_credits >= pkt_credits{                 |
1057    |     |                                 |                                                    |
1058    |     |                                 |    pipe_ov_credits -= pkt_credits;                 |
1059    |     |                                 |                                                    |
1060    |     |                                 | }                                                  |
1061    |     |                                 |                                                    |
1062    +-----+---------------------------------+----------------------------------------------------+
1063
1064 .. _table_qos_15:
1065
1066 .. table:: Watermark Calculation
1067
1068    +-----+------------------+----------------------------------------------------------------------------------+
1069    | No. | Subport Traffic  | Description                                                                      |
1070    |     | Class Operation  |                                                                                  |
1071    +=====+==================+==================================================================================+
1072    | 1   | Initialization   | **Subport level:**                                                               |
1073    |     |                  |                                                                                  |
1074    |     |                  | wm = WM_MAX                                                                      |
1075    |     |                  |                                                                                  |
1076    +-----+------------------+----------------------------------------------------------------------------------+
1077    | 2   | Credit update    | **Subport level (water_mark_update):**                                           |
1078    |     |                  |                                                                                  |
1079    |     |                  | tc0_cons = subport_tc0_credits_per_period - subport_tc0_credits;                 |
1080    |     |                  |                                                                                  |
1081    |     |                  | tc1_cons = subport_tc1_credits_per_period - subport_tc1_credits;                 |
1082    |     |                  |                                                                                  |
1083    |     |                  | tc2_cons = subport_tc2_credits_per_period - subport_tc2_credits;                 |
1084    |     |                  |                                                                                  |
1085    |     |                  | tc3_cons = subport_tc3_credits_per_period - subport_tc3_credits;                 |
1086    |     |                  |                                                                                  |
1087    |     |                  | tc3_cons_max = subport_tc3_credits_per_period - (tc0_cons + tc1_cons +           |
1088    |     |                  | tc2_cons);                                                                       |
1089    |     |                  |                                                                                  |
1090    |     |                  | if(tc3_consumption > (tc3_consumption_max - MTU)){                               |
1091    |     |                  |                                                                                  |
1092    |     |                  |     wm -= wm >> 7;                                                               |
1093    |     |                  |                                                                                  |
1094    |     |                  |     if(wm < WM_MIN) wm =  WM_MIN;                                                |
1095    |     |                  |                                                                                  |
1096    |     |                  | } else {                                                                         |
1097    |     |                  |                                                                                  |
1098    |     |                  |    wm += (wm >> 7) + 1;                                                          |
1099    |     |                  |                                                                                  |
1100    |     |                  |    if(wm > WM_MAX) wm = WM_MAX;                                                  |
1101    |     |                  |                                                                                  |
1102    |     |                  | }                                                                                |
1103    |     |                  |                                                                                  |
1104    +-----+------------------+----------------------------------------------------------------------------------+
1105
1106 Worst Case Scenarios for Performance
1107 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1108
1109 Lots of Active Queues with Not Enough Credits
1110 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1111
1112 The more queues the scheduler has to examine for packets and credits in order to select one packet,
1113 the lower the performance of the scheduler is.
1114
1115 The scheduler maintains the bitmap of active queues, which skips the non-active queues,
1116 but in order to detect whether a specific pipe has enough credits,
1117 the pipe has to be drilled down using the pipe dequeue state machine,
1118 which consumes cycles regardless of the scheduling result
1119 (no packets are produced or at least one packet is produced).
1120
1121 This scenario stresses the importance of the policer for the scheduler performance:
1122 if the pipe does not have enough credits,
1123 its packets should be dropped as soon as possible (before they reach the hierarchical scheduler),
1124 thus rendering the pipe queues as not active,
1125 which allows the dequeue side to skip that pipe with no cycles being spent on investigating the pipe credits
1126 that would result in a "not enough credits" status.
1127
1128 Single Queue with 100% Line Rate
1129 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1130
1131 The port scheduler performance is optimized for a large number of queues.
1132 If the number of queues is small,
1133 then the performance of the port scheduler for the same level of active traffic is expected to be worse than
1134 the performance of a small set of message passing queues.
1135
1136 .. _Dropper:
1137
1138 Dropper
1139 -------
1140
1141 The purpose of the DPDK dropper is to drop packets arriving at a packet scheduler to avoid congestion.
1142 The dropper supports the Random Early Detection (RED),
1143 Weighted Random Early Detection (WRED) and tail drop algorithms.
1144 :numref:`figure_blk_diag_dropper` illustrates how the dropper integrates with the scheduler.
1145 The DPDK currently does not support congestion management
1146 so the dropper provides the only method for congestion avoidance.
1147
1148 .. _figure_blk_diag_dropper:
1149
1150 .. figure:: img/blk_diag_dropper.*
1151
1152    High-level Block Diagram of the DPDK Dropper
1153
1154
1155 The dropper uses the Random Early Detection (RED) congestion avoidance algorithm as documented in the reference publication.
1156 The purpose of the RED algorithm is to monitor a packet queue,
1157 determine the current congestion level in the queue and decide whether an arriving packet should be enqueued or dropped.
1158 The RED algorithm uses an Exponential Weighted Moving Average (EWMA) filter to compute average queue size which
1159 gives an indication of the current congestion level in the queue.
1160
1161 For each enqueue operation, the RED algorithm compares the average queue size to minimum and maximum thresholds.
1162 Depending on whether the average queue size is below, above or in between these thresholds,
1163 the RED algorithm calculates the probability that an arriving packet should be dropped and
1164 makes a random decision based on this probability.
1165
1166 The dropper also supports Weighted Random Early Detection (WRED) by allowing the scheduler to select
1167 different RED configurations for the same packet queue at run-time.
1168 In the case of severe congestion, the dropper resorts to tail drop.
1169 This occurs when a packet queue has reached maximum capacity and cannot store any more packets.
1170 In this situation, all arriving packets are dropped.
1171
1172 The flow through the dropper is illustrated in :numref:`figure_flow_tru_droppper`.
1173 The RED/WRED algorithm is exercised first and tail drop second.
1174
1175 .. _figure_flow_tru_droppper:
1176
1177 .. figure:: img/flow_tru_droppper.*
1178
1179    Flow Through the Dropper
1180
1181
1182 The use cases supported by the dropper are:
1183
1184 *   *    Initialize configuration data
1185
1186 *   *    Initialize run-time data
1187
1188 *   *    Enqueue (make a decision to enqueue or drop an arriving packet)
1189
1190 *   *    Mark empty (record the time at which a packet queue becomes empty)
1191
1192 The configuration use case is explained in :ref:`Section2.23.3.1 <Configuration>`,
1193 the enqueue operation is explained in  :ref:`Section 2.23.3.2 <Enqueue_Operation>`
1194 and the mark empty operation is explained in :ref:`Section 2.23.3.3 <Queue_Empty_Operation>`.
1195
1196 .. _Configuration:
1197
1198 Configuration
1199 ~~~~~~~~~~~~~
1200
1201 A RED configuration contains the parameters given in :numref:`table_qos_16`.
1202
1203 .. _table_qos_16:
1204
1205 .. table:: RED Configuration Parameters
1206
1207    +--------------------------+---------+---------+------------------+
1208    | Parameter                | Minimum | Maximum | Typical          |
1209    |                          |         |         |                  |
1210    +==========================+=========+=========+==================+
1211    | Minimum Threshold        | 0       | 1022    | 1/4 x queue size |
1212    |                          |         |         |                  |
1213    +--------------------------+---------+---------+------------------+
1214    | Maximum Threshold        | 1       | 1023    | 1/2 x queue size |
1215    |                          |         |         |                  |
1216    +--------------------------+---------+---------+------------------+
1217    | Inverse Mark Probability | 1       | 255     | 10               |
1218    |                          |         |         |                  |
1219    +--------------------------+---------+---------+------------------+
1220    | EWMA Filter Weight       | 1       | 12      | 9                |
1221    |                          |         |         |                  |
1222    +--------------------------+---------+---------+------------------+
1223
1224 The meaning of these parameters is explained in more detail in the following sections.
1225 The format of these parameters as specified to the dropper module API
1226 corresponds to the format used by Cisco* in their RED implementation.
1227 The minimum and maximum threshold parameters are specified to the dropper module in terms of number of packets.
1228 The mark probability parameter is specified as an inverse value, for example,
1229 an inverse mark probability parameter value of 10 corresponds
1230 to a mark probability of 1/10 (that is, 1 in 10 packets will be dropped).
1231 The EWMA filter weight parameter is specified as an inverse log value,
1232 for example, a filter weight parameter value of 9 corresponds to a filter weight of 1/29.
1233
1234 .. _Enqueue_Operation:
1235
1236 Enqueue Operation
1237 ~~~~~~~~~~~~~~~~~
1238
1239 In the example shown in :numref:`figure_ex_data_flow_tru_dropper`, q (actual queue size) is the input value,
1240 avg (average queue size) and count (number of packets since the last drop) are run-time values,
1241 decision is the output value and the remaining values are configuration parameters.
1242
1243 .. _figure_ex_data_flow_tru_dropper:
1244
1245 .. figure:: img/ex_data_flow_tru_dropper.*
1246
1247    Example Data Flow Through Dropper
1248
1249
1250 EWMA Filter Microblock
1251 ^^^^^^^^^^^^^^^^^^^^^^
1252
1253 The purpose of the EWMA Filter microblock is to filter queue size values to smooth out transient changes
1254 that result from "bursty" traffic.
1255 The output value is the average queue size which gives a more stable view of the current congestion level in the queue.
1256
1257 The EWMA filter has one configuration parameter, filter weight, which determines how quickly
1258 or slowly the average queue size output responds to changes in the actual queue size input.
1259 Higher values of filter weight mean that the average queue size responds more quickly to changes in actual queue size.
1260
1261 Average Queue Size Calculation when the Queue is not Empty
1262 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""
1263
1264 The definition of the EWMA filter is given in the following equation.
1265
1266 .. image:: img/ewma_filter_eq_1.*
1267
1268 Where:
1269
1270 *   *avg*  = average queue size
1271
1272 *   *wq*   = filter weight
1273
1274 *   *q*    = actual queue size
1275
1276 .. note::
1277
1278     The filter weight, wq = 1/2^n, where n is the filter weight parameter value passed to the dropper module
1279         on configuration (see :ref:`Section2.23.3.1 <Configuration>` ).
1280
1281 Average Queue Size Calculation when the Queue is Empty
1282 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1283
1284 The EWMA filter does not read time stamps and instead assumes that enqueue operations will happen quite regularly.
1285 Special handling is required when the queue becomes empty as the queue could be empty for a short time or a long time.
1286 When the queue becomes empty, average queue size should decay gradually to zero instead of dropping suddenly to zero
1287 or remaining stagnant at the last computed value.
1288 When a packet is enqueued on an empty queue, the average queue size is computed using the following formula:
1289
1290 .. image:: img/ewma_filter_eq_2.*
1291
1292 Where:
1293
1294 *   *m*   = the number of enqueue operations that could have occurred on this queue while the queue was empty
1295
1296 In the dropper module, *m* is defined as:
1297
1298 .. image:: img/m_definition.*
1299
1300 Where:
1301
1302 *   *time*  = current time
1303
1304 *   *qtime* = time the queue became empty
1305
1306 *   *s* = typical time between successive enqueue operations on this queue
1307
1308 The time reference is in units of bytes,
1309 where a byte signifies the time duration required by the physical interface to send out a byte on the transmission medium
1310 (see Section `Internal Time Reference`_).
1311 The parameter s is defined in the dropper module as a constant with the value: s=2^22.
1312 This corresponds to the time required by every leaf node in a hierarchy with 64K leaf nodes
1313 to transmit one 64-byte packet onto the wire and represents the worst case scenario.
1314 For much smaller scheduler hierarchies,
1315 it may be necessary to reduce the parameter s, which is defined in the red header source file (rte_red.h) as:
1316
1317 .. code-block:: c
1318
1319     #define RTE_RED_S
1320
1321 Since the time reference is in bytes, the port speed is implied in the expression: *time-qtime*.
1322 The dropper does not have to be configured with the actual port speed.
1323 It adjusts automatically to low speed and high speed links.
1324
1325 Implementation
1326 """"""""""""""
1327
1328 A numerical method is used to compute the factor (1-wq)^m that appears in Equation 2.
1329
1330 This method is based on the following identity:
1331
1332 .. image:: img/eq2_factor.*
1333
1334
1335 This allows us to express the following:
1336
1337 .. image:: img/eq2_expression.*
1338
1339
1340 In the dropper module, a look-up table is used to compute log2(1-wq) for each value of wq supported by the dropper module.
1341 The factor (1-wq)^m can then be obtained by multiplying the table value by *m* and applying shift operations.
1342 To avoid overflow in the multiplication, the value, *m*, and the look-up table values are limited to 16 bits.
1343 The total size of the look-up table is 56 bytes.
1344 Once the factor (1-wq)^m is obtained using this method, the average queue size can be calculated from Equation 2.
1345
1346 Alternative Approaches
1347 """"""""""""""""""""""
1348
1349 Other methods for calculating the factor (1-wq)^m in the expression for computing average queue size
1350 when the queue is empty (Equation 2) were considered.
1351 These approaches include:
1352
1353 *   Floating-point evaluation
1354
1355 *   Fixed-point evaluation using a small look-up table (512B) and up to 16 multiplications
1356     (this is the approach used in the FreeBSD* ALTQ RED implementation)
1357
1358 *   Fixed-point evaluation using a small look-up table (512B) and 16 SSE multiplications
1359     (SSE optimized version of the approach used in the FreeBSD* ALTQ RED implementation)
1360
1361 *   Large look-up table (76 KB)
1362
1363 The method that was finally selected (described above in Section 26.3.2.2.1) out performs all of these approaches
1364 in terms of run-time performance and memory requirements and
1365 also achieves accuracy comparable to floating-point evaluation.
1366 :numref:`table_qos_17` lists the performance of each of these alternative approaches relative to the method that is used in the dropper.
1367 As can be seen, the floating-point implementation achieved the worst performance.
1368
1369 .. _table_qos_17:
1370
1371 .. table:: Relative Performance of Alternative Approaches
1372
1373    +------------------------------------------------------------------------------------+----------------------+
1374    | Method                                                                             | Relative Performance |
1375    |                                                                                    |                      |
1376    +====================================================================================+======================+
1377    | Current dropper method (see :ref:`Section 23.3.2.1.3 <Dropper>`)                   | 100%                 |
1378    |                                                                                    |                      |
1379    +------------------------------------------------------------------------------------+----------------------+
1380    | Fixed-point method with small (512B) look-up table                                 | 148%                 |
1381    |                                                                                    |                      |
1382    +------------------------------------------------------------------------------------+----------------------+
1383    | SSE method with small (512B) look-up table                                         | 114%                 |
1384    |                                                                                    |                      |
1385    +------------------------------------------------------------------------------------+----------------------+
1386    | Large (76KB) look-up table                                                         | 118%                 |
1387    |                                                                                    |                      |
1388    +------------------------------------------------------------------------------------+----------------------+
1389    | Floating-point                                                                     | 595%                 |
1390    |                                                                                    |                      |
1391    +------------------------------------------------------------------------------------+----------------------+
1392    | **Note**: In this case, since performance is expressed as time spent executing the operation in a         |
1393    | specific condition, any relative performance value above 100% runs slower than the reference method.      |
1394    |                                                                                                           |
1395    +-----------------------------------------------------------------------------------------------------------+
1396
1397 Drop Decision Block
1398 ^^^^^^^^^^^^^^^^^^^
1399
1400 The Drop Decision block:
1401
1402 *   Compares the average queue size with the minimum and maximum thresholds
1403
1404 *   Calculates a packet drop probability
1405
1406 *   Makes a random decision to enqueue or drop an arriving packet
1407
1408 The calculation of the drop probability occurs in two stages.
1409 An initial drop probability is calculated based on the average queue size,
1410 the minimum and maximum thresholds and the mark probability.
1411 An actual drop probability is then computed from the initial drop probability.
1412 The actual drop probability takes the count run-time value into consideration
1413 so that the actual drop probability increases as more packets arrive to the packet queue
1414 since the last packet was dropped.
1415
1416 Initial Packet Drop Probability
1417 """""""""""""""""""""""""""""""
1418
1419 The initial drop probability is calculated using the following equation.
1420
1421 .. image:: img/drop_probability_eq3.*
1422
1423 Where:
1424
1425 *   *maxp*  = mark probability
1426
1427 *   *avg*  = average queue size
1428
1429 *   *minth*  = minimum threshold
1430
1431 *   *maxth*  = maximum threshold
1432
1433 The calculation of the packet drop probability using Equation 3 is illustrated in :numref:`figure_pkt_drop_probability`.
1434 If the average queue size is below the minimum threshold, an arriving packet is enqueued.
1435 If the average queue size is at or above the maximum threshold, an arriving packet is dropped.
1436 If the average queue size is between the minimum and maximum thresholds,
1437 a drop probability is calculated to determine if the packet should be enqueued or dropped.
1438
1439 .. _figure_pkt_drop_probability:
1440
1441 .. figure:: img/pkt_drop_probability.*
1442
1443    Packet Drop Probability for a Given RED Configuration
1444
1445
1446 Actual Drop Probability
1447 """""""""""""""""""""""
1448
1449 If the average queue size is between the minimum and maximum thresholds,
1450 then the actual drop probability is calculated from the following equation.
1451
1452 .. image:: img/drop_probability_eq4.*
1453
1454 Where:
1455
1456 *   *Pb*  = initial drop probability (from Equation 3)
1457
1458 *   *count* = number of packets that have arrived since the last drop
1459
1460 The constant 2, in Equation 4 is the only deviation from the drop probability formulae
1461 given in the reference document where a value of 1 is used instead.
1462 It should be noted that the value pa computed from can be negative or greater than 1.
1463 If this is the case, then a value of 1 should be used instead.
1464
1465 The initial and actual drop probabilities are shown in :numref:`figure_drop_probability_graph`.
1466 The actual drop probability is shown for the case where
1467 the formula given in the reference document1 is used (blue curve)
1468 and also for the case where the formula implemented in the dropper module,
1469 is used (red curve).
1470 The formula in the reference document results in a significantly higher drop rate
1471 compared to the mark probability configuration parameter specified by the user.
1472 The choice to deviate from the reference document is simply a design decision and
1473 one that has been taken by other RED implementations, for example, FreeBSD* ALTQ RED.
1474
1475 .. _figure_drop_probability_graph:
1476
1477 .. figure:: img/drop_probability_graph.*
1478
1479    Initial Drop Probability (pb), Actual Drop probability (pa) Computed Using
1480    a Factor 1 (Blue Curve) and a Factor 2 (Red Curve)
1481
1482
1483 .. _Queue_Empty_Operation:
1484
1485 Queue Empty Operation
1486 ~~~~~~~~~~~~~~~~~~~~~
1487
1488 The time at which a packet queue becomes empty must be recorded and saved with the RED run-time data
1489 so that the EWMA filter block can calculate the average queue size on the next enqueue operation.
1490 It is the responsibility of the calling application to inform the dropper module
1491 through the API that a queue has become empty.
1492
1493 Source Files Location
1494 ~~~~~~~~~~~~~~~~~~~~~
1495
1496 The source files for the DPDK dropper are located at:
1497
1498 *   DPDK/lib/librte_sched/rte_red.h
1499
1500 *   DPDK/lib/librte_sched/rte_red.c
1501
1502 Integration with the DPDK QoS Scheduler
1503 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1504
1505 RED functionality in the DPDK QoS scheduler is disabled by default.
1506 To enable it, use the DPDK configuration parameter:
1507
1508 ::
1509
1510     CONFIG_RTE_SCHED_RED=y
1511
1512 This parameter must be set to y.
1513 The parameter is found in the build configuration files in the DPDK/config directory,
1514 for example, DPDK/config/common_linux.
1515 RED configuration parameters are specified in the rte_red_params structure within the rte_sched_port_params structure
1516 that is passed to the scheduler on initialization.
1517 RED parameters are specified separately for four traffic classes and three packet colors (green, yellow and red)
1518 allowing the scheduler to implement Weighted Random Early Detection (WRED).
1519
1520 Integration with the DPDK QoS Scheduler Sample Application
1521 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1522
1523 The DPDK QoS Scheduler Application reads a configuration file on start-up.
1524 The configuration file includes a section containing RED parameters.
1525 The format of these parameters is described in :ref:`Section2.23.3.1 <Configuration>`.
1526 A sample RED configuration is shown below. In this example, the queue size is 64 packets.
1527
1528 .. note::
1529
1530     For correct operation, the same EWMA filter weight parameter (wred weight) should be used
1531     for each packet color (green, yellow, red) in the same traffic class (tc).
1532
1533 ::
1534
1535     ; RED params per traffic class and color (Green / Yellow / Red)
1536
1537    [red]
1538    tc 0 wred min = 28 22 16
1539    tc 0 wred max = 32 32 32
1540    tc 0 wred inv prob = 10 10 10
1541    tc 0 wred weight = 9 9 9
1542
1543    tc 1 wred min = 28 22 16
1544    tc 1 wred max = 32 32 32
1545    tc 1 wred inv prob = 10 10 10
1546    tc 1 wred weight = 9 9 9
1547
1548    tc 2 wred min = 28 22 16
1549    tc 2 wred max = 32 32 32
1550    tc 2 wred inv prob = 10 10 10
1551    tc 2 wred weight = 9 9 9
1552
1553    tc 3 wred min = 28 22 16
1554    tc 3 wred max = 32 32 32
1555    tc 3 wred inv prob = 10 10 10
1556    tc 3 wred weight = 9 9 9
1557
1558 With this configuration file, the RED configuration that applies to green,
1559 yellow and red packets in traffic class 0 is shown in :numref:`table_qos_18`.
1560
1561 .. _table_qos_18:
1562
1563 .. table:: RED Configuration Corresponding to RED Configuration File
1564
1565    +--------------------+--------------------+-------+--------+-----+
1566    | RED Parameter      | Configuration Name | Green | Yellow | Red |
1567    |                    |                    |       |        |     |
1568    +====================+====================+=======+========+=====+
1569    | Minimum Threshold  | tc 0 wred min      | 28    | 22     | 16  |
1570    |                    |                    |       |        |     |
1571    +--------------------+--------------------+-------+--------+-----+
1572    | Maximum Threshold  | tc 0 wred max      | 32    | 32     | 32  |
1573    |                    |                    |       |        |     |
1574    +--------------------+--------------------+-------+--------+-----+
1575    | Mark Probability   | tc 0 wred inv prob | 10    | 10     | 10  |
1576    |                    |                    |       |        |     |
1577    +--------------------+--------------------+-------+--------+-----+
1578    | EWMA Filter Weight | tc 0 wred weight   | 9     | 9      | 9   |
1579    |                    |                    |       |        |     |
1580    +--------------------+--------------------+-------+--------+-----+
1581
1582 Application Programming Interface (API)
1583 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1584
1585 Enqueue API
1586 ^^^^^^^^^^^
1587
1588 The syntax of the enqueue API is as follows:
1589
1590 .. code-block:: c
1591
1592    int rte_red_enqueue(const struct rte_red_config *red_cfg, struct rte_red *red, const unsigned q, const uint64_t time)
1593
1594
1595 The arguments passed to the enqueue API are configuration data, run-time data,
1596 the current size of the packet queue (in packets) and a value representing the current time.
1597 The time reference is in units of bytes,
1598 where a byte signifies the time duration required by the physical interface to send out a byte on the transmission medium
1599 (see Section 26.2.4.5.1 "Internal Time Reference" ).
1600 The dropper reuses the scheduler time stamps for performance reasons.
1601
1602 Empty API
1603 ^^^^^^^^^
1604
1605 The syntax of the empty API is as follows:
1606
1607 .. code-block:: c
1608
1609     void rte_red_mark_queue_empty(struct rte_red *red, const uint64_t time)
1610
1611 The arguments passed to the empty API are run-time data and the current time in bytes.
1612
1613 Traffic Metering
1614 ----------------
1615
1616 The traffic metering component implements the Single Rate Three Color Marker (srTCM) and
1617 Two Rate Three Color Marker (trTCM) algorithms, as defined by IETF RFC 2697 and 2698 respectively.
1618 These algorithms meter the stream of incoming packets based on the allowance defined in advance for each traffic flow.
1619 As result, each incoming packet is tagged as green,
1620 yellow or red based on the monitored consumption of the flow the packet belongs to.
1621
1622 Functional Overview
1623 ~~~~~~~~~~~~~~~~~~~
1624
1625 The srTCM algorithm defines two token buckets for each traffic flow,
1626 with the two buckets sharing the same token update rate:
1627
1628 *   Committed (C) bucket: fed with tokens at the rate defined by the Committed Information Rate (CIR) parameter
1629     (measured in IP packet bytes per second).
1630     The size of the C bucket is defined by the Committed Burst Size (CBS) parameter (measured in bytes);
1631
1632 *   Excess (E) bucket: fed with tokens at the same rate as the C bucket.
1633     The size of the E bucket is defined by the Excess Burst Size (EBS) parameter (measured in bytes).
1634
1635 The trTCM algorithm defines two token buckets for each traffic flow,
1636 with the two buckets being updated with tokens at independent rates:
1637
1638 *   Committed (C) bucket: fed with tokens at the rate defined by the Committed Information Rate (CIR) parameter
1639     (measured in bytes of IP packet per second).
1640     The size of the C bucket is defined by the Committed Burst Size (CBS) parameter (measured in bytes);
1641
1642 *   Peak (P) bucket: fed with tokens at the rate defined by the Peak Information Rate (PIR) parameter
1643     (measured in IP packet bytes per second).
1644     The size of the P bucket is defined by the Peak Burst Size (PBS) parameter (measured in bytes).
1645
1646 Please refer to RFC 2697 (for srTCM) and RFC 2698 (for trTCM) for details on how tokens are consumed
1647 from the buckets and how the packet color is determined.
1648
1649 Color Blind and Color Aware Modes
1650 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1651
1652 For both algorithms, the color blind mode is functionally equivalent to the color aware mode with input color set as green.
1653 For color aware mode, a packet with red input color can only get the red output color,
1654 while a packet with yellow input color can only get the yellow or red output colors.
1655
1656 The reason why the color blind mode is still implemented distinctly than the color aware mode is
1657 that color blind mode can be implemented with fewer operations than the color aware mode.
1658
1659 Implementation Overview
1660 ~~~~~~~~~~~~~~~~~~~~~~~
1661
1662 For each input packet, the steps for the srTCM / trTCM algorithms are:
1663
1664 *   Update the C and E / P token buckets. This is done by reading the current time (from the CPU timestamp counter),
1665     identifying the amount of time since the last bucket update and computing the associated number of tokens
1666     (according to the pre-configured bucket rate).
1667     The number of tokens in the bucket is limited by the pre-configured bucket size;
1668
1669 *   Identify the output color for the current packet based on the size of the IP packet
1670     and the amount of tokens currently available in the C and E / P buckets; for color aware mode only,
1671     the input color of the packet is also considered.
1672     When the output color is not red, a number of tokens equal to the length of the IP packet are
1673     subtracted from the C or E /P or both buckets, depending on the algorithm and the output color of the packet.