New upstream version 18.08
[deb_dpdk.git] / lib / librte_eal / common / include / rte_service.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #ifndef _RTE_SERVICE_H_
6 #define _RTE_SERVICE_H_
7
8 /**
9  * @file
10  *
11  * Service functions
12  *
13  * The service functionality provided by this header allows a DPDK component
14  * to indicate that it requires a function call in order for it to perform
15  * its processing.
16  *
17  * An example usage of this functionality would be a component that registers
18  * a service to perform a particular packet processing duty: for example the
19  * eventdev software PMD. At startup the application requests all services
20  * that have been registered, and the cores in the service-coremask run the
21  * required services. The EAL removes these number of cores from the available
22  * runtime cores, and dedicates them to performing service-core workloads. The
23  * application has access to the remaining lcores as normal.
24  */
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #include<stdio.h>
31 #include <stdint.h>
32 #include <sys/queue.h>
33
34 #include <rte_config.h>
35 #include <rte_lcore.h>
36
37 #define RTE_SERVICE_NAME_MAX 32
38
39 /* Capabilities of a service.
40  *
41  * Use the *rte_service_probe_capability* function to check if a service is
42  * capable of a specific capability.
43  */
44 /** When set, the service is capable of having multiple threads run it at the
45  *  same time.
46  */
47 #define RTE_SERVICE_CAP_MT_SAFE (1 << 0)
48
49 /**
50  *  Return the number of services registered.
51  *
52  * The number of services registered can be passed to *rte_service_get_by_id*,
53  * enabling the application to retrieve the specification of each service.
54  *
55  * @return The number of services registered.
56  */
57 uint32_t rte_service_get_count(void);
58
59 /**
60  * Return the id of a service by name.
61  *
62  * This function provides the id of the service using the service name as
63  * lookup key. The service id is to be passed to other functions in the
64  * rte_service_* API.
65  *
66  * Example usage:
67  * @code
68  *      uint32_t service_id;
69  *      int32_t ret = rte_service_get_by_name("service_X", &service_id);
70  *      if (ret) {
71  *              // handle error
72  *      }
73  * @endcode
74  *
75  * @param name The name of the service to retrieve
76  * @param[out] service_id A pointer to a uint32_t, to be filled in with the id.
77  * @retval 0 Success. The service id is provided in *service_id*.
78  * @retval -EINVAL Null *service_id* pointer provided
79  * @retval -ENODEV No such service registered
80  */
81 int32_t rte_service_get_by_name(const char *name, uint32_t *service_id);
82
83 /**
84  * Return the name of the service.
85  *
86  * @return A pointer to the name of the service. The returned pointer remains
87  *         in ownership of the service, and the application must not free it.
88  */
89 const char *rte_service_get_name(uint32_t id);
90
91 /**
92  * Check if a service has a specific capability.
93  *
94  * This function returns if *service* has implements *capability*.
95  * See RTE_SERVICE_CAP_* defines for a list of valid capabilities.
96  * @retval 1 Capability supported by this service instance
97  * @retval 0 Capability not supported by this service instance
98  */
99 int32_t rte_service_probe_capability(uint32_t id, uint32_t capability);
100
101 /**
102  * Map or unmap a lcore to a service.
103  *
104  * Each core can be added or removed from running a specific service. This
105  * function enables or disables *lcore* to run *service_id*.
106  *
107  * If multiple cores are enabled on a service, an atomic is used to ensure that
108  * only one cores runs the service at a time. The exception to this is when
109  * a service indicates that it is multi-thread safe by setting the capability
110  * called RTE_SERVICE_CAP_MT_SAFE. With the multi-thread safe capability set,
111  * the service function can be run on multiple threads at the same time.
112  *
113  * @param service_id the service to apply the lcore to
114  * @param lcore The lcore that will be mapped to service
115  * @param enable Zero to unmap or disable the core, non-zero to enable
116  *
117  * @retval 0 lcore map updated successfully
118  * @retval -EINVAL An invalid service or lcore was provided.
119  */
120 int32_t rte_service_map_lcore_set(uint32_t service_id, uint32_t lcore,
121                 uint32_t enable);
122
123 /**
124  * Retrieve the mapping of an lcore to a service.
125  *
126  * @param service_id the service to apply the lcore to
127  * @param lcore The lcore that will be mapped to service
128  *
129  * @retval 1 lcore is mapped to service
130  * @retval 0 lcore is not mapped to service
131  * @retval -EINVAL An invalid service or lcore was provided.
132  */
133 int32_t rte_service_map_lcore_get(uint32_t service_id, uint32_t lcore);
134
135 /**
136  * Set the runstate of the service.
137  *
138  * Each service is either running or stopped. Setting a non-zero runstate
139  * enables the service to run, while setting runstate zero disables it.
140  *
141  * @param id The id of the service
142  * @param runstate The run state to apply to the service
143  *
144  * @retval 0 The service was successfully started
145  * @retval -EINVAL Invalid service id
146  */
147 int32_t rte_service_runstate_set(uint32_t id, uint32_t runstate);
148
149 /**
150  * Get the runstate for the service with *id*. See *rte_service_runstate_set*
151  * for details of runstates. A service can call this function to ensure that
152  * the application has indicated that it will receive CPU cycles. Either a
153  * service-core is mapped (default case), or the application has explicitly
154  * disabled the check that a service-cores is mapped to the service and takes
155  * responsibility to run the service manually using the available function
156  * *rte_service_run_iter_on_app_lcore* to do so.
157  *
158  * @retval 1 Service is running
159  * @retval 0 Service is stopped
160  * @retval -EINVAL Invalid service id
161  */
162 int32_t rte_service_runstate_get(uint32_t id);
163
164 /**
165  * @warning
166  * @b EXPERIMENTAL: this API may change, or be removed, without prior notice
167  *
168  * This function returns whether the service may be currently executing on
169  * at least one lcore, or definitely is not. This function can be used to
170  * determine if, after setting the service runstate to stopped, the service
171  * is still executing a service lcore.
172  *
173  * Care must be taken if calling this function when the service runstate is
174  * running, since the result of this function may be incorrect by the time the
175  * function returns due to service cores running in parallel.
176  *
177  * @retval 1 Service may be running on one or more lcores
178  * @retval 0 Service is not running on any lcore
179  * @retval -EINVAL Invalid service id
180  */
181 int32_t __rte_experimental
182 rte_service_may_be_active(uint32_t id);
183
184 /**
185  * Enable or disable the check for a service-core being mapped to the service.
186  * An application can disable the check when takes the responsibility to run a
187  * service itself using *rte_service_run_iter_on_app_lcore*.
188  *
189  * @param id The id of the service to set the check on
190  * @param enable When zero, the check is disabled. Non-zero enables the check.
191  *
192  * @retval 0 Success
193  * @retval -EINVAL Invalid service ID
194  */
195 int32_t rte_service_set_runstate_mapped_check(uint32_t id, int32_t enable);
196
197 /**
198  * This function runs a service callback from a non-service lcore.
199  *
200  * This function is designed to enable gradual porting to service cores, and
201  * to enable unit tests to verify a service behaves as expected.
202  *
203  * When called, this function ensures that the service identified by *id* is
204  * safe to run on this lcore. Multi-thread safe services are invoked even if
205  * other cores are simultaneously running them as they are multi-thread safe.
206  *
207  * Multi-thread unsafe services are handled depending on the variable
208  * *serialize_multithread_unsafe*:
209  * - When set, the function will check if a service is already being invoked
210  *   on another lcore, refusing to run it and returning -EBUSY.
211  * - When zero, the application takes responsibility to ensure that the service
212  *   indicated by *id* is not going to be invoked by another lcore. This setting
213  *   avoids atomic operations, so is likely to be more performant.
214  *
215  * @param id The ID of the service to run
216  * @param serialize_multithread_unsafe This parameter indicates to the service
217  *           cores library if it is required to use atomics to serialize access
218  *           to mult-thread unsafe services. As there is an overhead in using
219  *           atomics, applications can choose to enable or disable this feature
220  *
221  * Note that any thread calling this function MUST be a DPDK EAL thread, as
222  * the *rte_lcore_id* function is used to access internal data structures.
223  *
224  * @retval 0 Service was run on the calling thread successfully
225  * @retval -EBUSY Another lcore is executing the service, and it is not a
226  *         multi-thread safe service, so the service was not run on this lcore
227  * @retval -ENOEXEC Service is not in a run-able state
228  * @retval -EINVAL Invalid service id
229  */
230 int32_t rte_service_run_iter_on_app_lcore(uint32_t id,
231                 uint32_t serialize_multithread_unsafe);
232
233 /**
234  * Start a service core.
235  *
236  * Starting a core makes the core begin polling. Any services assigned to it
237  * will be run as fast as possible. The application must ensure that the lcore
238  * is in a launchable state: e.g. call *rte_eal_lcore_wait* on the lcore_id
239  * before calling this function.
240  *
241  * @retval 0 Success
242  * @retval -EINVAL Failed to start core. The *lcore_id* passed in is not
243  *          currently assigned to be a service core.
244  */
245 int32_t rte_service_lcore_start(uint32_t lcore_id);
246
247 /**
248  * Stop a service core.
249  *
250  * Stopping a core makes the core become idle, but remains  assigned as a
251  * service core.
252  *
253  * @retval 0 Success
254  * @retval -EINVAL Invalid *lcore_id* provided
255  * @retval -EALREADY Already stopped core
256  * @retval -EBUSY Failed to stop core, as it would cause a service to not
257  *          be run, as this is the only core currently running the service.
258  *          The application must stop the service first, and then stop the
259  *          lcore.
260  */
261 int32_t rte_service_lcore_stop(uint32_t lcore_id);
262
263 /**
264  * Adds lcore to the list of service cores.
265  *
266  * This functions can be used at runtime in order to modify the service core
267  * mask.
268  *
269  * @retval 0 Success
270  * @retval -EBUSY lcore is busy, and not available for service core duty
271  * @retval -EALREADY lcore is already added to the service core list
272  * @retval -EINVAL Invalid lcore provided
273  */
274 int32_t rte_service_lcore_add(uint32_t lcore);
275
276 /**
277  * Removes lcore from the list of service cores.
278  *
279  * This can fail if the core is not stopped, see *rte_service_core_stop*.
280  *
281  * @retval 0 Success
282  * @retval -EBUSY Lcore is not stopped, stop service core before removing.
283  * @retval -EINVAL failed to add lcore to service core mask.
284  */
285 int32_t rte_service_lcore_del(uint32_t lcore);
286
287 /**
288  * Retrieve the number of service cores currently available.
289  *
290  * This function returns the integer count of service cores available. The
291  * service core count can be used in mapping logic when creating mappings
292  * from service cores to services.
293  *
294  * See *rte_service_lcore_list* for details on retrieving the lcore_id of each
295  * service core.
296  *
297  * @return The number of service cores currently configured.
298  */
299 int32_t rte_service_lcore_count(void);
300
301 /**
302  * Resets all service core mappings. This does not remove the service cores
303  * from duty, just unmaps all services / cores, and stops() the service cores.
304  * The runstate of services is not modified.
305  *
306  * @retval 0 Success
307  */
308 int32_t rte_service_lcore_reset_all(void);
309
310 /**
311  * Enable or disable statistics collection for *service*.
312  *
313  * This function enables per core, per-service cycle count collection.
314  * @param id The service to enable statistics gathering on.
315  * @param enable Zero to disable statistics, non-zero to enable.
316  * @retval 0 Success
317  * @retval -EINVAL Invalid service pointer passed
318  */
319 int32_t rte_service_set_stats_enable(uint32_t id, int32_t enable);
320
321 /**
322  * Retrieve the list of currently enabled service cores.
323  *
324  * This function fills in an application supplied array, with each element
325  * indicating the lcore_id of a service core.
326  *
327  * Adding and removing service cores can be performed using
328  * *rte_service_lcore_add* and *rte_service_lcore_del*.
329  * @param [out] array An array of at least *rte_service_lcore_count* items.
330  *              If statically allocating the buffer, use RTE_MAX_LCORE.
331  * @param [out] n The size of *array*.
332  * @retval >=0 Number of service cores that have been populated in the array
333  * @retval -ENOMEM The provided array is not large enough to fill in the
334  *          service core list. No items have been populated, call this function
335  *          with a size of at least *rte_service_core_count* items.
336  */
337 int32_t rte_service_lcore_list(uint32_t array[], uint32_t n);
338
339 /**
340  * Get the numer of services running on the supplied lcore.
341  *
342  * @param lcore Id of the service core.
343  * @retval >=0 Number of services registered to this core.
344  * @retval -EINVAL Invalid lcore provided
345  * @retval -ENOTSUP The provided lcore is not a service core.
346  */
347 int32_t rte_service_lcore_count_services(uint32_t lcore);
348
349 /**
350  * Dumps any information available about the service. When id is UINT32_MAX,
351  * this function dumps info for all services.
352  *
353  * @retval 0 Statistics have been successfully dumped
354  * @retval -EINVAL Invalid service id provided
355  */
356 int32_t rte_service_dump(FILE *f, uint32_t id);
357
358 /**
359  * Returns the number of cycles that this service has consumed
360  */
361 #define RTE_SERVICE_ATTR_CYCLES 0
362
363 /**
364  * Returns the count of invocations of this service function
365  */
366 #define RTE_SERVICE_ATTR_CALL_COUNT 1
367
368 /**
369  * Get an attribute from a service.
370  *
371  * @retval 0 Success, the attribute value has been written to *attr_value*.
372  *         -EINVAL Invalid id, attr_id or attr_value was NULL.
373  */
374 int32_t rte_service_attr_get(uint32_t id, uint32_t attr_id,
375                 uint32_t *attr_value);
376
377 /**
378  * Reset all attribute values of a service.
379  *
380  * @param id The service to reset all statistics of
381  * @retval 0 Successfully reset attributes
382  *         -EINVAL Invalid service id provided
383  */
384 int32_t rte_service_attr_reset_all(uint32_t id);
385
386 /**
387  * Returns the number of times the service runner has looped.
388  */
389 #define RTE_SERVICE_LCORE_ATTR_LOOPS 0
390
391 /**
392  * @warning
393  * @b EXPERIMENTAL: this API may change without prior notice
394  *
395  * Get an attribute from a service core.
396  *
397  * @param lcore Id of the service core.
398  * @param attr_id Id of the attribute to be retrieved.
399  * @param [out] attr_value Pointer to storage in which to write retrieved value.
400  * @retval 0 Success, the attribute value has been written to *attr_value*.
401  *         -EINVAL Invalid lcore, attr_id or attr_value was NULL.
402  *         -ENOTSUP lcore is not a service core.
403  */
404 int32_t __rte_experimental
405 rte_service_lcore_attr_get(uint32_t lcore, uint32_t attr_id,
406                            uint64_t *attr_value);
407
408 /**
409  * @warning
410  * @b EXPERIMENTAL: this API may change without prior notice
411  *
412  * Reset all attribute values of a service core.
413  *
414  * @param lcore The service core to reset all the statistics of
415  * @retval 0 Successfully reset attributes
416  *         -EINVAL Invalid service id provided
417  *         -ENOTSUP lcore is not a service core.
418  */
419 int32_t __rte_experimental
420 rte_service_lcore_attr_reset_all(uint32_t lcore);
421
422 #ifdef __cplusplus
423 }
424 #endif
425
426
427 #endif /* _RTE_SERVICE_H_ */