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