New upstream version 18.02
[deb_dpdk.git] / doc / guides / prog_guide / eventdev.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright(c) 2017 Intel Corporation.
3
4 Event Device Library
5 ====================
6
7 The DPDK Event device library is an abstraction that provides the application
8 with features to schedule events. This is achieved using the PMD architecture
9 similar to the ethdev or cryptodev APIs, which may already be familiar to the
10 reader.
11
12 The eventdev framework introduces the event driven programming model. In a
13 polling model, lcores poll ethdev ports and associated Rx queues directly
14 to look for a packet. By contrast in an event driven model, lcores call the
15 scheduler that selects packets for them based on programmer-specified criteria.
16 The Eventdev library adds support for an event driven programming model, which
17 offers applications automatic multicore scaling, dynamic load balancing,
18 pipelining, packet ingress order maintenance and synchronization services to
19 simplify application packet processing.
20
21 By introducing an event driven programming model, DPDK can support both polling
22 and event driven programming models for packet processing, and applications are
23 free to choose whatever model (or combination of the two) best suits their
24 needs.
25
26 Step-by-step instructions of the eventdev design is available in the `API
27 Walk-through`_ section later in this document.
28
29 Event struct
30 ------------
31
32 The eventdev API represents each event with a generic struct, which contains a
33 payload and metadata required for scheduling by an eventdev.  The
34 ``rte_event`` struct is a 16 byte C structure, defined in
35 ``libs/librte_eventdev/rte_eventdev.h``.
36
37 Event Metadata
38 ~~~~~~~~~~~~~~
39
40 The rte_event structure contains the following metadata fields, which the
41 application fills in to have the event scheduled as required:
42
43 * ``flow_id`` - The targeted flow identifier for the enq/deq operation.
44 * ``event_type`` - The source of this event, eg RTE_EVENT_TYPE_ETHDEV or CPU.
45 * ``sub_event_type`` - Distinguishes events inside the application, that have
46   the same event_type (see above)
47 * ``op`` - This field takes one of the RTE_EVENT_OP_* values, and tells the
48   eventdev about the status of the event - valid values are NEW, FORWARD or
49   RELEASE.
50 * ``sched_type`` - Represents the type of scheduling that should be performed
51   on this event, valid values are the RTE_SCHED_TYPE_ORDERED, ATOMIC and
52   PARALLEL.
53 * ``queue_id`` - The identifier for the event queue that the event is sent to.
54 * ``priority`` - The priority of this event, see RTE_EVENT_DEV_PRIORITY.
55
56 Event Payload
57 ~~~~~~~~~~~~~
58
59 The rte_event struct contains a union for payload, allowing flexibility in what
60 the actual event being scheduled is. The payload is a union of the following:
61
62 * ``uint64_t u64``
63 * ``void *event_ptr``
64 * ``struct rte_mbuf *mbuf``
65
66 These three items in a union occupy the same 64 bits at the end of the rte_event
67 structure. The application can utilize the 64 bits directly by accessing the
68 u64 variable, while the event_ptr and mbuf are provided as convenience
69 variables.  For example the mbuf pointer in the union can used to schedule a
70 DPDK packet.
71
72 Queues
73 ~~~~~~
74
75 An event queue is a queue containing events that are scheduled by the event
76 device. An event queue contains events of different flows associated with
77 scheduling types, such as atomic, ordered, or parallel.
78
79 Queue All Types Capable
80 ^^^^^^^^^^^^^^^^^^^^^^^
81
82 If RTE_EVENT_DEV_CAP_QUEUE_ALL_TYPES capability bit is set in the event device,
83 then events of any type may be sent to any queue. Otherwise, the queues only
84 support events of the type that it was created with.
85
86 Queue All Types Incapable
87 ^^^^^^^^^^^^^^^^^^^^^^^^^
88
89 In this case, each stage has a specified scheduling type.  The application
90 configures each queue for a specific type of scheduling, and just enqueues all
91 events to the eventdev. An example of a PMD of this type is the eventdev
92 software PMD.
93
94 The Eventdev API supports the following scheduling types per queue:
95
96 *   Atomic
97 *   Ordered
98 *   Parallel
99
100 Atomic, Ordered and Parallel are load-balanced scheduling types: the output
101 of the queue can be spread out over multiple CPU cores.
102
103 Atomic scheduling on a queue ensures that a single flow is not present on two
104 different CPU cores at the same time. Ordered allows sending all flows to any
105 core, but the scheduler must ensure that on egress the packets are returned to
106 ingress order on downstream queue enqueue. Parallel allows sending all flows
107 to all CPU cores, without any re-ordering guarantees.
108
109 Single Link Flag
110 ^^^^^^^^^^^^^^^^
111
112 There is a SINGLE_LINK flag which allows an application to indicate that only
113 one port will be connected to a queue.  Queues configured with the single-link
114 flag follow a FIFO like structure, maintaining ordering but it is only capable
115 of being linked to a single port (see below for port and queue linking details).
116
117
118 Ports
119 ~~~~~
120
121 Ports are the points of contact between worker cores and the eventdev. The
122 general use-case will see one CPU core using one port to enqueue and dequeue
123 events from an eventdev. Ports are linked to queues in order to retrieve events
124 from those queues (more details in `Linking Queues and Ports`_ below).
125
126
127 API Walk-through
128 ----------------
129
130 This section will introduce the reader to the eventdev API, showing how to
131 create and configure an eventdev and use it for a two-stage atomic pipeline
132 with a single core for TX. The diagram below shows the final state of the
133 application after this walk-through:
134
135 .. _figure_eventdev-usage1:
136
137 .. figure:: img/eventdev_usage.*
138
139    Sample eventdev usage, with RX, two atomic stages and a single-link to TX.
140
141
142 A high level overview of the setup steps are:
143
144 * rte_event_dev_configure()
145 * rte_event_queue_setup()
146 * rte_event_port_setup()
147 * rte_event_port_link()
148 * rte_event_dev_start()
149
150
151 Init and Config
152 ~~~~~~~~~~~~~~~
153
154 The eventdev library uses vdev options to add devices to the DPDK application.
155 The ``--vdev`` EAL option allows adding eventdev instances to your DPDK
156 application, using the name of the eventdev PMD as an argument.
157
158 For example, to create an instance of the software eventdev scheduler, the
159 following vdev arguments should be provided to the application EAL command line:
160
161 .. code-block:: console
162
163    ./dpdk_application --vdev="event_sw0"
164
165 In the following code, we configure eventdev instance with 3 queues
166 and 6 ports as follows. The 3 queues consist of 2 Atomic and 1 Single-Link,
167 while the 6 ports consist of 4 workers, 1 RX and 1 TX.
168
169 .. code-block:: c
170
171         const struct rte_event_dev_config config = {
172                 .nb_event_queues = 3,
173                 .nb_event_ports = 6,
174                 .nb_events_limit  = 4096,
175                 .nb_event_queue_flows = 1024,
176                 .nb_event_port_dequeue_depth = 128,
177                 .nb_event_port_enqueue_depth = 128,
178         };
179         int err = rte_event_dev_configure(dev_id, &config);
180
181 The remainder of this walk-through assumes that dev_id is 0.
182
183 Setting up Queues
184 ~~~~~~~~~~~~~~~~~
185
186 Once the eventdev itself is configured, the next step is to configure queues.
187 This is done by setting the appropriate values in a queue_conf structure, and
188 calling the setup function. Repeat this step for each queue, starting from
189 0 and ending at ``nb_event_queues - 1`` from the event_dev config above.
190
191 .. code-block:: c
192
193         struct rte_event_queue_conf atomic_conf = {
194                 .schedule_type = RTE_SCHED_TYPE_ATOMIC,
195                 .priority = RTE_EVENT_DEV_PRIORITY_NORMAL,
196                 .nb_atomic_flows = 1024,
197                 .nb_atomic_order_sequences = 1024,
198         };
199         int dev_id = 0;
200         int queue_id = 0;
201         int err = rte_event_queue_setup(dev_id, queue_id, &atomic_conf);
202
203 The remainder of this walk-through assumes that the queues are configured as
204 follows:
205
206  * id 0, atomic queue #1
207  * id 1, atomic queue #2
208  * id 2, single-link queue
209
210 Setting up Ports
211 ~~~~~~~~~~~~~~~~
212
213 Once queues are set up successfully, create the ports as required. Each port
214 should be set up with its corresponding port_conf type, worker for worker cores,
215 rx and tx for the RX and TX cores:
216
217 .. code-block:: c
218
219         struct rte_event_port_conf rx_conf = {
220                 .dequeue_depth = 128,
221                 .enqueue_depth = 128,
222                 .new_event_threshold = 1024,
223         };
224         struct rte_event_port_conf worker_conf = {
225                 .dequeue_depth = 16,
226                 .enqueue_depth = 64,
227                 .new_event_threshold = 4096,
228         };
229         struct rte_event_port_conf tx_conf = {
230                 .dequeue_depth = 128,
231                 .enqueue_depth = 128,
232                 .new_event_threshold = 4096,
233         };
234         int dev_id = 0;
235         int port_id = 0;
236         int err = rte_event_port_setup(dev_id, port_id, &CORE_FUNCTION_conf);
237
238 It is now assumed that:
239
240  * port 0: RX core
241  * ports 1,2,3,4: Workers
242  * port 5: TX core
243
244 Linking Queues and Ports
245 ~~~~~~~~~~~~~~~~~~~~~~~~
246
247 The final step is to "wire up" the ports to the queues. After this, the
248 eventdev is capable of scheduling events, and when cores request work to do,
249 the correct events are provided to that core. Note that the RX core takes input
250 from eg: a NIC so it is not linked to any eventdev queues.
251
252 Linking all workers to atomic queues, and the TX core to the single-link queue
253 can be achieved like this:
254
255 .. code-block:: c
256
257         uint8_t port_id = 0;
258         uint8_t atomic_qs[] = {0, 1};
259         uint8_t single_link_q = 2;
260         uint8_t tx_port_id = 5;
261         uin8t_t priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
262
263         for(int i = 0; i < 4; i++) {
264                 int worker_port = i + 1;
265                 int links_made = rte_event_port_link(dev_id, worker_port, atomic_qs, NULL, 2);
266         }
267         int links_made = rte_event_port_link(dev_id, tx_port_id, &single_link_q, &priority, 1);
268
269 Starting the EventDev
270 ~~~~~~~~~~~~~~~~~~~~~
271
272 A single function call tells the eventdev instance to start processing
273 events. Note that all queues must be linked to for the instance to start, as
274 if any queue is not linked to, enqueuing to that queue will cause the
275 application to backpressure and eventually stall due to no space in the
276 eventdev.
277
278 .. code-block:: c
279
280         int err = rte_event_dev_start(dev_id);
281
282 Ingress of New Events
283 ~~~~~~~~~~~~~~~~~~~~~
284
285 Now that the eventdev is set up, and ready to receive events, the RX core must
286 enqueue some events into the system for it to schedule. The events to be
287 scheduled are ordinary DPDK packets, received from an eth_rx_burst() as normal.
288 The following code shows how those packets can be enqueued into the eventdev:
289
290 .. code-block:: c
291
292         const uint16_t nb_rx = rte_eth_rx_burst(eth_port, 0, mbufs, BATCH_SIZE);
293
294         for (i = 0; i < nb_rx; i++) {
295                 ev[i].flow_id = mbufs[i]->hash.rss;
296                 ev[i].op = RTE_EVENT_OP_NEW;
297                 ev[i].sched_type = RTE_SCHED_TYPE_ATOMIC;
298                 ev[i].queue_id = 0;
299                 ev[i].event_type = RTE_EVENT_TYPE_ETHDEV;
300                 ev[i].sub_event_type = 0;
301                 ev[i].priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
302                 ev[i].mbuf = mbufs[i];
303         }
304
305         const int nb_tx = rte_event_enqueue_burst(dev_id, port_id, ev, nb_rx);
306         if (nb_tx != nb_rx) {
307                 for(i = nb_tx; i < nb_rx; i++)
308                         rte_pktmbuf_free(mbufs[i]);
309         }
310
311 Forwarding of Events
312 ~~~~~~~~~~~~~~~~~~~~
313
314 Now that the RX core has injected events, there is work to be done by the
315 workers. Note that each worker will dequeue as many events as it can in a burst,
316 process each one individually, and then burst the packets back into the
317 eventdev.
318
319 The worker can lookup the events source from ``event.queue_id``, which should
320 indicate to the worker what workload needs to be performed on the event.
321 Once done, the worker can update the ``event.queue_id`` to a new value, to send
322 the event to the next stage in the pipeline.
323
324 .. code-block:: c
325
326         int timeout = 0;
327         struct rte_event events[BATCH_SIZE];
328         uint16_t nb_rx = rte_event_dequeue_burst(dev_id, worker_port_id, events, BATCH_SIZE, timeout);
329
330         for (i = 0; i < nb_rx; i++) {
331                 /* process mbuf using events[i].queue_id as pipeline stage */
332                 struct rte_mbuf *mbuf = events[i].mbuf;
333                 /* Send event to next stage in pipeline */
334                 events[i].queue_id++;
335         }
336
337         uint16_t nb_tx = rte_event_enqueue_burst(dev_id, port_id, events, nb_rx);
338
339
340 Egress of Events
341 ~~~~~~~~~~~~~~~~
342
343 Finally, when the packet is ready for egress or needs to be dropped, we need
344 to inform the eventdev that the packet is no longer being handled by the
345 application. This can be done by calling dequeue() or dequeue_burst(), which
346 indicates that the previous burst of packets is no longer in use by the
347 application.
348
349 An event driven worker thread has following typical workflow on fastpath:
350
351 .. code-block:: c
352
353        while (1) {
354                rte_event_dequeue_burst(...);
355                (event processing)
356                rte_event_enqueue_burst(...);
357        }
358
359
360 Summary
361 -------
362
363 The eventdev library allows an application to easily schedule events as it
364 requires, either using a run-to-completion or pipeline processing model.  The
365 queues and ports abstract the logical functionality of an eventdev, providing
366 the application with a generic method to schedule events.  With the flexible
367 PMD infrastructure applications benefit of improvements in existing eventdevs
368 and additions of new ones without modification.