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