xref: /dpdk/lib/eal/include/rte_service.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
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 #include<stdio.h>
27 #include <stdint.h>
28 
29 #include <rte_config.h>
30 #include <rte_lcore.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
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  * Return the number of services registered.
50  *
51  * @return The number of services registered.
52  */
53 uint32_t rte_service_get_count(void);
54 
55 /**
56  * Return the id of a service by name.
57  *
58  * This function provides the id of the service using the service name as
59  * lookup key. The service id is to be passed to other functions in the
60  * rte_service_* API.
61  *
62  * Example usage:
63  * @code
64  *      uint32_t service_id;
65  *      int32_t ret = rte_service_get_by_name("service_X", &service_id);
66  *      if (ret) {
67  *              // handle error
68  *      }
69  * @endcode
70  *
71  * @param name The name of the service to retrieve
72  * @param[out] service_id A pointer to a uint32_t, to be filled in with the id.
73  * @retval 0 Success. The service id is provided in *service_id*.
74  * @retval -EINVAL Null *service_id* pointer provided
75  * @retval -ENODEV No such service registered
76  */
77 int32_t rte_service_get_by_name(const char *name, uint32_t *service_id);
78 
79 /**
80  * Return the name of the service.
81  *
82  * @return A pointer to the name of the service. The returned pointer remains
83  *         in ownership of the service, and the application must not free it.
84  */
85 const char *rte_service_get_name(uint32_t id);
86 
87 /**
88  * Check if a service has a specific capability.
89  *
90  * This function returns if *service* has implements *capability*.
91  * See RTE_SERVICE_CAP_* defines for a list of valid capabilities.
92  * @retval 1 Capability supported by this service instance
93  * @retval 0 Capability not supported by this service instance
94  */
95 int32_t rte_service_probe_capability(uint32_t id, uint32_t capability);
96 
97 /**
98  * Map or unmap a lcore to a service.
99  *
100  * Each core can be added or removed from running a specific service. This
101  * function enables or disables *lcore* to run *service_id*.
102  *
103  * If multiple cores are enabled on a service, a lock is used to ensure that
104  * only one core runs the service at a time. The exception to this is when
105  * a service indicates that it is multi-thread safe by setting the capability
106  * called RTE_SERVICE_CAP_MT_SAFE. With the multi-thread safe capability set,
107  * the service function can be run on multiple threads at the same time.
108  *
109  * If the service is known to be mapped to a single lcore, setting the
110  * capability of the service to RTE_SERVICE_CAP_MT_SAFE can achieve
111  * better performance by avoiding the use of lock.
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  * This function returns whether the service may be currently executing on
166  * at least one lcore, or definitely is not. This function can be used to
167  * determine if, after setting the service runstate to stopped, the service
168  * is still executing a service lcore.
169  *
170  * Care must be taken if calling this function when the service runstate is
171  * running, since the result of this function may be incorrect by the time the
172  * function returns due to service cores running in parallel.
173  *
174  * @retval 1 Service may be running on one or more lcores
175  * @retval 0 Service is not running on any lcore
176  * @retval -EINVAL Invalid service id
177  */
178 int32_t
179 rte_service_may_be_active(uint32_t id);
180 
181 /**
182  * Enable or disable the check for a service-core being mapped to the service.
183  * An application can disable the check when takes the responsibility to run a
184  * service itself using rte_service_run_iter_on_app_lcore().
185  *
186  * @param id The id of the service to set the check on
187  * @param enable When zero, the check is disabled. Non-zero enables the check.
188  *
189  * @retval 0 Success
190  * @retval -EINVAL Invalid service ID
191  */
192 int32_t rte_service_set_runstate_mapped_check(uint32_t id, int32_t enable);
193 
194 /**
195  * This function runs a service callback from a non-service lcore.
196  *
197  * This function is designed to enable gradual porting to service cores, and
198  * to enable unit tests to verify a service behaves as expected.
199  *
200  * When called, this function ensures that the service identified by *id* is
201  * safe to run on this lcore. Multi-thread safe services are invoked even if
202  * other cores are simultaneously running them as they are multi-thread safe.
203  *
204  * Multi-thread unsafe services are handled depending on the variable
205  * *serialize_multithread_unsafe*:
206  * - When set, the function will check if a service is already being invoked
207  *   on another lcore, refusing to run it and returning -EBUSY.
208  * - When zero, the application takes responsibility to ensure that the service
209  *   indicated by *id* is not going to be invoked by another lcore. This setting
210  *   avoids atomic operations, so is likely to be more performant.
211  *
212  * @param id The ID of the service to run
213  * @param serialize_multithread_unsafe This parameter indicates to the service
214  *           cores library if it is required to use atomics to serialize access
215  *           to mult-thread unsafe services. As there is an overhead in using
216  *           atomics, applications can choose to enable or disable this feature
217  *
218  * Note that any thread calling this function MUST be a DPDK EAL thread, as
219  * the rte_lcore_id() function is used to access internal data structures.
220  *
221  * @retval 0 Service was run on the calling thread successfully
222  * @retval -EBUSY Another lcore is executing the service, and it is not a
223  *         multi-thread safe service, so the service was not run on this lcore
224  * @retval -ENOEXEC Service is not in a run-able state
225  * @retval -EINVAL Invalid service id
226  */
227 int32_t rte_service_run_iter_on_app_lcore(uint32_t id,
228 		uint32_t serialize_multithread_unsafe);
229 
230 /**
231  * Start a service core.
232  *
233  * Starting a core makes the core begin polling. Any services assigned to it
234  * will be run as fast as possible. The application must ensure that the lcore
235  * is in a launchable state: e.g. call rte_eal_lcore_wait() on the lcore_id
236  * before calling this function.
237  *
238  * @retval 0 Success
239  * @retval -EINVAL Failed to start core. The *lcore_id* passed in is not
240  *          currently assigned to be a service core.
241  */
242 int32_t rte_service_lcore_start(uint32_t lcore_id);
243 
244 /**
245  * Stop a service core.
246  *
247  * Stopping a core makes the core become idle, but remains  assigned as a
248  * service core. Note that the service lcore thread may not have returned from
249  * the service it is running when this API returns.
250  *
251  * The rte_service_lcore_may_be_active() API can be used to check if the
252  * service lcore is * still active.
253  *
254  * @retval 0 Success
255  * @retval -EINVAL Invalid *lcore_id* provided
256  * @retval -EALREADY Already stopped core
257  * @retval -EBUSY Failed to stop core, as it would cause a service to not
258  *          be run, as this is the only core currently running the service.
259  *          The application must stop the service first, and then stop the
260  *          lcore.
261  */
262 int32_t rte_service_lcore_stop(uint32_t lcore_id);
263 
264 /**
265  * Reports if a service lcore is currently running.
266  *
267  * This function returns if the core has finished service cores code, and has
268  * returned to EAL control. If rte_service_lcore_stop() has been called but
269  * the lcore has not returned to EAL yet, it might be required to wait and call
270  * this function again. The amount of time to wait before the core returns
271  * depends on the duration of the services being run.
272  *
273  * @retval 0 Service thread is not active, and lcore has been returned to EAL.
274  * @retval 1 Service thread is in the service core polling loop.
275  * @retval -EINVAL Invalid *lcore_id* provided.
276  */
277 int32_t rte_service_lcore_may_be_active(uint32_t lcore_id);
278 
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  * Removes lcore from the list of service cores.
294  *
295  * This can fail if the core is not stopped, see rte_service_core_stop().
296  *
297  * @retval 0 Success
298  * @retval -EBUSY Lcore is not stopped, stop service core before removing.
299  * @retval -EINVAL failed to add lcore to service core mask.
300  */
301 int32_t rte_service_lcore_del(uint32_t lcore);
302 
303 /**
304  * Retrieve the number of service cores currently available.
305  *
306  * This function returns the integer count of service cores available. The
307  * service core count can be used in mapping logic when creating mappings
308  * from service cores to services.
309  *
310  * See rte_service_lcore_list() for details on retrieving the lcore_id of each
311  * service core.
312  *
313  * @return The number of service cores currently configured.
314  */
315 int32_t rte_service_lcore_count(void);
316 
317 /**
318  * Resets all service core mappings. This does not remove the service cores
319  * from duty, just unmaps all services / cores, and stops() the service cores.
320  * The runstate of services is not modified.
321  *
322  * The cores that are stopped with this call, are in WAIT state.
323  *
324  * @retval 0 Success
325  */
326 int32_t rte_service_lcore_reset_all(void);
327 
328 /**
329  * Enable or disable statistics collection for *service*.
330  *
331  * This function enables per core, per-service cycle count collection.
332  * @param id The service to enable statistics gathering on.
333  * @param enable Zero to disable statistics, non-zero to enable.
334  * @retval 0 Success
335  * @retval -EINVAL Invalid service pointer passed
336  */
337 int32_t rte_service_set_stats_enable(uint32_t id, int32_t enable);
338 
339 /**
340  * Retrieve the list of currently enabled service cores.
341  *
342  * This function fills in an application supplied array, with each element
343  * indicating the lcore_id of a service core.
344  *
345  * Adding and removing service cores can be performed using
346  * rte_service_lcore_add() and rte_service_lcore_del().
347  * @param [out] array An array of at least rte_service_lcore_count() items.
348  *              If statically allocating the buffer, use RTE_MAX_LCORE.
349  * @param [out] n The size of *array*.
350  * @retval >=0 Number of service cores that have been populated in the array
351  * @retval -ENOMEM The provided array is not large enough to fill in the
352  *          service core list. No items have been populated, call this function
353  *          with a size of at least rte_service_core_count() items.
354  */
355 int32_t rte_service_lcore_list(uint32_t array[], uint32_t n);
356 
357 /**
358  * Get the number of services running on the supplied lcore.
359  *
360  * @param lcore Id of the service core.
361  * @retval >=0 Number of services registered to this core.
362  * @retval -EINVAL Invalid lcore provided
363  * @retval -ENOTSUP The provided lcore is not a service core.
364  */
365 int32_t rte_service_lcore_count_services(uint32_t lcore);
366 
367 /**
368  * Dumps any information available about the service. When id is UINT32_MAX,
369  * this function dumps info for all services.
370  *
371  * @retval 0 Statistics have been successfully dumped
372  * @retval -EINVAL Invalid service id provided
373  */
374 int32_t rte_service_dump(FILE *f, uint32_t id);
375 
376 /**
377  * Returns the number of cycles that this service has consumed. Only
378  * cycles spent in non-idle calls (i.e., calls not returning -EAGAIN)
379  * count.
380  */
381 #define RTE_SERVICE_ATTR_CYCLES 0
382 
383 /**
384  * Returns the total number of invocations of this service function
385  * (regardless of return value).
386  */
387 #define RTE_SERVICE_ATTR_CALL_COUNT 1
388 
389 /**
390  * Returns the number of invocations of this service function where the
391  * service reported having not performed any useful work (i.e.,
392  * returned -EAGAIN).
393  */
394 #define RTE_SERVICE_ATTR_IDLE_CALL_COUNT 2
395 
396 /**
397  * Returns the number of invocations of this service function where the
398  * service reported an error (i.e., the return value was neither 0 nor
399  * -EAGAIN).
400  */
401 #define RTE_SERVICE_ATTR_ERROR_CALL_COUNT 3
402 
403 /**
404  * Get an attribute from a service.
405  *
406  * @retval 0 Success, the attribute value has been written to *attr_value*.
407  *         -EINVAL Invalid id, attr_id or attr_value was NULL.
408  */
409 int32_t rte_service_attr_get(uint32_t id, uint32_t attr_id,
410 		uint64_t *attr_value);
411 
412 /**
413  * Reset all attribute values of a service.
414  *
415  * @param id The service to reset all statistics of
416  * @retval 0 Successfully reset attributes
417  *         -EINVAL Invalid service id provided
418  */
419 int32_t rte_service_attr_reset_all(uint32_t id);
420 
421 /**
422  * Returns the number of times the service runner has looped.
423  */
424 #define RTE_SERVICE_LCORE_ATTR_LOOPS 0
425 
426 /**
427  * Returns the total number of cycles that the lcore has spent on
428  * running services. Only non-idle calls (i.e., calls not returning
429  * -EAGAIN) count toward this total.
430  */
431 #define RTE_SERVICE_LCORE_ATTR_CYCLES 1
432 
433 /**
434  * Get an attribute from a service core.
435  *
436  * @param lcore Id of the service core.
437  * @param attr_id Id of the attribute to be retrieved.
438  * @param [out] attr_value Pointer to storage in which to write retrieved value.
439  * @retval 0 Success, the attribute value has been written to *attr_value*.
440  *         -EINVAL Invalid lcore, attr_id or attr_value was NULL.
441  *         -ENOTSUP lcore is not a service core.
442  */
443 int32_t
444 rte_service_lcore_attr_get(uint32_t lcore, uint32_t attr_id,
445 			   uint64_t *attr_value);
446 
447 /**
448  * Reset all attribute values of a service core.
449  *
450  * @param lcore The service core to reset all the statistics of
451  * @retval 0 Successfully reset attributes
452  *         -EINVAL Invalid service id provided
453  *         -ENOTSUP lcore is not a service core.
454  */
455 int32_t
456 rte_service_lcore_attr_reset_all(uint32_t lcore);
457 
458 #ifdef __cplusplus
459 }
460 #endif
461 
462 
463 #endif /* _RTE_SERVICE_H_ */
464