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