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