service: allow to disable core check
[dpdk.git] / lib / librte_eal / common / include / rte_service.h
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef _RTE_SERVICE_H_
34 #define _RTE_SERVICE_H_
35
36 /**
37  * @file
38  *
39  * Service functions
40  *
41  * The service functionality provided by this header allows a DPDK component
42  * to indicate that it requires a function call in order for it to perform
43  * its processing.
44  *
45  * An example usage of this functionality would be a component that registers
46  * a service to perform a particular packet processing duty: for example the
47  * eventdev software PMD. At startup the application requests all services
48  * that have been registered, and the cores in the service-coremask run the
49  * required services. The EAL removes these number of cores from the available
50  * runtime cores, and dedicates them to performing service-core workloads. The
51  * application has access to the remaining lcores as normal.
52  */
53
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57
58 #include<stdio.h>
59 #include <stdint.h>
60 #include <sys/queue.h>
61
62 #include <rte_lcore.h>
63
64 #define RTE_SERVICE_NAME_MAX 32
65
66 /* Capabilities of a service.
67  *
68  * Use the *rte_service_probe_capability* function to check if a service is
69  * capable of a specific capability.
70  */
71 /** When set, the service is capable of having multiple threads run it at the
72  *  same time.
73  */
74 #define RTE_SERVICE_CAP_MT_SAFE (1 << 0)
75
76 /**
77  * @warning
78  * @b EXPERIMENTAL: this API may change without prior notice
79  *
80  *  Return the number of services registered.
81  *
82  * The number of services registered can be passed to *rte_service_get_by_id*,
83  * enabling the application to retrieve the specification of each service.
84  *
85  * @return The number of services registered.
86  */
87 uint32_t rte_service_get_count(void);
88
89 /**
90  * @warning
91  * @b EXPERIMENTAL: this API may change without prior notice
92  *
93  * Return the id of a service by name.
94  *
95  * This function provides the id of the service using the service name as
96  * lookup key. The service id is to be passed to other functions in the
97  * rte_service_* API.
98  *
99  * Example usage:
100  * @code
101  *      uint32_t service_id;
102  *      int32_t ret = rte_service_get_by_name("service_X", &service_id);
103  *      if (ret) {
104  *              // handle error
105  *      }
106  * @endcode
107  *
108  * @param name The name of the service to retrieve
109  * @param[out] service_id A pointer to a uint32_t, to be filled in with the id.
110  * @retval 0 Success. The service id is provided in *service_id*.
111  * @retval -EINVAL Null *service_id* pointer provided
112  * @retval -ENODEV No such service registered
113  */
114 int32_t rte_service_get_by_name(const char *name, uint32_t *service_id);
115
116 /**
117  * @warning
118  * @b EXPERIMENTAL: this API may change without prior notice
119  *
120  * Return the name of the service.
121  *
122  * @return A pointer to the name of the service. The returned pointer remains
123  *         in ownership of the service, and the application must not free it.
124  */
125 const char *rte_service_get_name(uint32_t id);
126
127 /**
128  * @warning
129  * @b EXPERIMENTAL: this API may change without prior notice
130  *
131  * Check if a service has a specific capability.
132  *
133  * This function returns if *service* has implements *capability*.
134  * See RTE_SERVICE_CAP_* defines for a list of valid capabilities.
135  * @retval 1 Capability supported by this service instance
136  * @retval 0 Capability not supported by this service instance
137  */
138 int32_t rte_service_probe_capability(uint32_t id, uint32_t capability);
139
140 /**
141  * @warning
142  * @b EXPERIMENTAL: this API may change without prior notice
143  *
144  * Map or unmap a lcore to a service.
145  *
146  * Each core can be added or removed from running a specific service. This
147  * function enables or disables *lcore* to run *service_id*.
148  *
149  * If multiple cores are enabled on a service, an atomic is used to ensure that
150  * only one cores runs the service at a time. The exception to this is when
151  * a service indicates that it is multi-thread safe by setting the capability
152  * called RTE_SERVICE_CAP_MT_SAFE. With the multi-thread safe capability set,
153  * the service function can be run on multiple threads at the same time.
154  *
155  * @param service_id the service to apply the lcore to
156  * @param lcore The lcore that will be mapped to service
157  * @param enable Zero to unmap or disable the core, non-zero to enable
158  *
159  * @retval 0 lcore map updated successfully
160  * @retval -EINVAL An invalid service or lcore was provided.
161  */
162 int32_t rte_service_map_lcore_set(uint32_t service_id, uint32_t lcore,
163                                   uint32_t enable);
164
165 /**
166  * @warning
167  * @b EXPERIMENTAL: this API may change without prior notice
168  *
169  * Retrieve the mapping of an lcore to a service.
170  *
171  * @param service_id the service to apply the lcore to
172  * @param lcore The lcore that will be mapped to service
173  *
174  * @retval 1 lcore is mapped to service
175  * @retval 0 lcore is not mapped to service
176  * @retval -EINVAL An invalid service or lcore was provided.
177  */
178 int32_t rte_service_map_lcore_get(uint32_t service_id, uint32_t lcore);
179
180 /**
181  * @warning
182  * @b EXPERIMENTAL: this API may change without prior notice
183  *
184  * Set the runstate of the service.
185  *
186  * Each service is either running or stopped. Setting a non-zero runstate
187  * enables the service to run, while setting runstate zero disables it.
188  *
189  * @param id The id of the service
190  * @param runstate The run state to apply to the service
191  *
192  * @retval 0 The service was successfully started
193  * @retval -EINVAL Invalid service id
194  */
195 int32_t rte_service_runstate_set(uint32_t id, uint32_t runstate);
196
197 /**
198  * @warning
199  * @b EXPERIMENTAL: this API may change without prior notice
200  *
201  * Get the runstate for the service with *id*. See *rte_service_runstate_set*
202  * for details of runstates. A service can call this function to ensure that
203  * the application has indicated that it will receive CPU cycles. Either a
204  * service-core is mapped (default case), or the application has explicitly
205  * disabled the check that a service-cores is mapped to the service and takes
206  * responsibility to run the service manually using the available function
207  * *rte_service_run_iter_on_app_lcore* to do so.
208  *
209  * @retval 1 Service is running
210  * @retval 0 Service is stopped
211  * @retval -EINVAL Invalid service id
212  */
213 int32_t rte_service_runstate_get(uint32_t id);
214
215 /**
216  * @warning
217  * @b EXPERIMENTAL: this API may change without prior notice
218  *
219  * Enable or disable the check for a service-core being mapped to the service.
220  * An application can disable the check when takes the responsibility to run a
221  * service itself using *rte_service_run_iter_on_app_lcore*.
222  *
223  * @param id The id of the service to set the check on
224  * @param enable When zero, the check is disabled. Non-zero enables the check.
225  *
226  * @retval 0 Success
227  * @retval -EINVAL Invalid service ID
228  */
229 int32_t rte_service_set_runstate_mapped_check(uint32_t id, int32_t enable);
230
231 /**
232  * @warning
233  * @b EXPERIMENTAL: this API may change without prior notice
234  *
235  * This function runs a service callback from a non-service lcore context.
236  * The *id* of the service to be run is passed in, and the service-callback
237  * is executed on the calling lcore immediately if possible. If the service is
238  * not multi-thread capable and another thread is currently executing it, this
239  * function returns without running the callback.
240  *
241  * Note that any thread calling this function MUST be a DPDK EAL thread, as
242  * the *rte_lcore_id* function is used to access internal data structures.
243  *
244  * @retval 0 Service was run on the calling thread successfully
245  * @retval -EBUSY Another lcore is executing the service, and it is not a
246  *         multi-thread safe service, so the service was not run on this lcore
247  * @retval -ENOEXEC Service is not in a runnable state
248  * @retval -EINVAL Invalid service id
249  */
250 int32_t rte_service_run_iter_on_app_lcore(uint32_t id);
251
252 /**
253  * @warning
254  * @b EXPERIMENTAL: this API may change without prior notice
255  *
256  * Start a service core.
257  *
258  * Starting a core makes the core begin polling. Any services assigned to it
259  * will be run as fast as possible.
260  *
261  * @retval 0 Success
262  * @retval -EINVAL Failed to start core. The *lcore_id* passed in is not
263  *          currently assigned to be a service core.
264  */
265 int32_t rte_service_lcore_start(uint32_t lcore_id);
266
267 /**
268  * @warning
269  * @b EXPERIMENTAL: this API may change without prior notice
270  *
271  * Stop a service core.
272  *
273  * Stopping a core makes the core become idle, but remains  assigned as a
274  * service core.
275  *
276  * @retval 0 Success
277  * @retval -EINVAL Invalid *lcore_id* provided
278  * @retval -EALREADY Already stopped core
279  * @retval -EBUSY Failed to stop core, as it would cause a service to not
280  *          be run, as this is the only core currently running the service.
281  *          The application must stop the service first, and then stop the
282  *          lcore.
283  */
284 int32_t rte_service_lcore_stop(uint32_t lcore_id);
285
286 /**
287  * @warning
288  * @b EXPERIMENTAL: this API may change without prior notice
289  *
290  * Adds lcore to the list of service cores.
291  *
292  * This functions can be used at runtime in order to modify the service core
293  * mask.
294  *
295  * @retval 0 Success
296  * @retval -EBUSY lcore is busy, and not available for service core duty
297  * @retval -EALREADY lcore is already added to the service core list
298  * @retval -EINVAL Invalid lcore provided
299  */
300 int32_t rte_service_lcore_add(uint32_t lcore);
301
302 /**
303  * @warning
304  * @b EXPERIMENTAL: this API may change without prior notice
305  *
306  * Removes lcore from the list of service cores.
307  *
308  * This can fail if the core is not stopped, see *rte_service_core_stop*.
309  *
310  * @retval 0 Success
311  * @retval -EBUSY Lcore is not stopped, stop service core before removing.
312  * @retval -EINVAL failed to add lcore to service core mask.
313  */
314 int32_t rte_service_lcore_del(uint32_t lcore);
315
316 /**
317  * @warning
318  * @b EXPERIMENTAL: this API may change without prior notice
319  *
320  * Retrieve the number of service cores currently available.
321  *
322  * This function returns the integer count of service cores available. The
323  * service core count can be used in mapping logic when creating mappings
324  * from service cores to services.
325  *
326  * See *rte_service_lcore_list* for details on retrieving the lcore_id of each
327  * service core.
328  *
329  * @return The number of service cores currently configured.
330  */
331 int32_t rte_service_lcore_count(void);
332
333 /**
334  * @warning
335  * @b EXPERIMENTAL: this API may change without prior notice
336  *
337  * Resets all service core mappings. This does not remove the service cores
338  * from duty, just unmaps all services / cores, and stops() the service cores.
339  * The runstate of services is not modified.
340  *
341  * @retval 0 Success
342  */
343 int32_t rte_service_lcore_reset_all(void);
344
345 /**
346  * @warning
347  * @b EXPERIMENTAL: this API may change without prior notice
348  *
349  * Enable or disable statistics collection for *service*.
350  *
351  * This function enables per core, per-service cycle count collection.
352  * @param id The service to enable statistics gathering on.
353  * @param enable Zero to disable statistics, non-zero to enable.
354  * @retval 0 Success
355  * @retval -EINVAL Invalid service pointer passed
356  */
357 int32_t rte_service_set_stats_enable(uint32_t id, int32_t enable);
358
359 /**
360  * @warning
361  * @b EXPERIMENTAL: this API may change without prior notice
362  *
363  * Retrieve the list of currently enabled service cores.
364  *
365  * This function fills in an application supplied array, with each element
366  * indicating the lcore_id of a service core.
367  *
368  * Adding and removing service cores can be performed using
369  * *rte_service_lcore_add* and *rte_service_lcore_del*.
370  * @param [out] array An array of at least *rte_service_lcore_count* items.
371  *              If statically allocating the buffer, use RTE_MAX_LCORE.
372  * @param [out] n The size of *array*.
373  * @retval >=0 Number of service cores that have been populated in the array
374  * @retval -ENOMEM The provided array is not large enough to fill in the
375  *          service core list. No items have been populated, call this function
376  *          with a size of at least *rte_service_core_count* items.
377  */
378 int32_t rte_service_lcore_list(uint32_t array[], uint32_t n);
379
380 /**
381  * @warning
382  * @b EXPERIMENTAL: this API may change without prior notice
383  *
384  * Get the numer of services running on the supplied lcore.
385  *
386  * @param lcore Id of the service core.
387  * @retval >=0 Number of services registered to this core.
388  * @retval -EINVAL Invalid lcore provided
389  * @retval -ENOTSUP The provided lcore is not a service core.
390  */
391 int32_t rte_service_lcore_count_services(uint32_t lcore);
392
393 /**
394  * @warning
395  * @b EXPERIMENTAL: this API may change without prior notice
396  *
397  * Dumps any information available about the service. When id is UINT32_MAX,
398  * this function dumps info for all services.
399  *
400  * @retval 0 Statistics have been successfully dumped
401  * @retval -EINVAL Invalid service id provided
402  */
403 int32_t rte_service_dump(FILE *f, uint32_t id);
404
405 #ifdef __cplusplus
406 }
407 #endif
408
409
410 #endif /* _RTE_SERVICE_H_ */