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