1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 5 #ifndef _RTE_LCORE_H_ 6 #define _RTE_LCORE_H_ 7 8 /** 9 * @file 10 * 11 * API for lcore and socket manipulation 12 * 13 */ 14 #include <rte_config.h> 15 #include <rte_per_lcore.h> 16 #include <rte_eal.h> 17 #include <rte_launch.h> 18 #include <rte_thread.h> 19 20 #ifdef __cplusplus 21 extern "C" { 22 #endif 23 24 #define LCORE_ID_ANY UINT32_MAX /**< Any lcore. */ 25 26 RTE_DECLARE_PER_LCORE(unsigned, _lcore_id); /**< Per thread "lcore id". */ 27 28 /** 29 * The lcore role (used in RTE or not). 30 */ 31 enum rte_lcore_role_t { 32 ROLE_RTE, 33 ROLE_OFF, 34 ROLE_SERVICE, 35 ROLE_NON_EAL, 36 }; 37 38 /** 39 * Get a lcore's role. 40 * 41 * @param lcore_id 42 * The identifier of the lcore, which MUST be between 0 and RTE_MAX_LCORE-1. 43 * @return 44 * The role of the lcore. 45 */ 46 enum rte_lcore_role_t rte_eal_lcore_role(unsigned int lcore_id); 47 48 /** 49 * Test if the core supplied has a specific role 50 * 51 * @param lcore_id 52 * The identifier of the lcore, which MUST be between 0 and 53 * RTE_MAX_LCORE-1. 54 * @param role 55 * The role to be checked against. 56 * @return 57 * Boolean value: positive if test is true; otherwise returns 0. 58 */ 59 int 60 rte_lcore_has_role(unsigned int lcore_id, enum rte_lcore_role_t role); 61 62 /** 63 * Return the Application thread ID of the execution unit. 64 * 65 * Note: in most cases the lcore id returned here will also correspond 66 * to the processor id of the CPU on which the thread is pinned, this 67 * will not be the case if the user has explicitly changed the thread to 68 * core affinities using --lcores EAL argument e.g. --lcores '(0-3)@10' 69 * to run threads with lcore IDs 0, 1, 2 and 3 on physical core 10.. 70 * 71 * @return 72 * Logical core ID (in EAL thread or registered non-EAL thread) or 73 * LCORE_ID_ANY (in unregistered non-EAL thread) 74 */ 75 static inline unsigned 76 rte_lcore_id(void) 77 { 78 return RTE_PER_LCORE(_lcore_id); 79 } 80 81 /** 82 * Get the id of the main lcore 83 * 84 * @return 85 * the id of the main lcore 86 */ 87 unsigned int rte_get_main_lcore(void); 88 89 /** 90 * Return the number of execution units (lcores) on the system. 91 * 92 * @return 93 * the number of execution units (lcores) on the system. 94 */ 95 unsigned int rte_lcore_count(void); 96 97 /** 98 * Return the index of the lcore starting from zero. 99 * 100 * When option -c or -l is given, the index corresponds 101 * to the order in the list. 102 * For example: 103 * -c 0x30, lcore 4 has index 0, and 5 has index 1. 104 * -l 22,18 lcore 22 has index 0, and 18 has index 1. 105 * 106 * @param lcore_id 107 * The targeted lcore, or -1 for the current one. 108 * @return 109 * The relative index, or -1 if not enabled. 110 */ 111 int rte_lcore_index(int lcore_id); 112 113 /** 114 * Return the ID of the physical socket of the logical core we are 115 * running on. 116 * @return 117 * the ID of current lcoreid's physical socket 118 */ 119 unsigned int rte_socket_id(void); 120 121 /** 122 * Return number of physical sockets detected on the system. 123 * 124 * Note that number of nodes may not be correspondent to their physical id's: 125 * for example, a system may report two socket id's, but the actual socket id's 126 * may be 0 and 8. 127 * 128 * @return 129 * the number of physical sockets as recognized by EAL 130 */ 131 unsigned int 132 rte_socket_count(void); 133 134 /** 135 * Return socket id with a particular index. 136 * 137 * This will return socket id at a particular position in list of all detected 138 * physical socket id's. For example, on a machine with sockets [0, 8], passing 139 * 1 as a parameter will return 8. 140 * 141 * @param idx 142 * index of physical socket id to return 143 * 144 * @return 145 * - physical socket id as recognized by EAL 146 * - -1 on error, with errno set to EINVAL 147 */ 148 int 149 rte_socket_id_by_idx(unsigned int idx); 150 151 /** 152 * Get the ID of the physical socket of the specified lcore 153 * 154 * @param lcore_id 155 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1. 156 * @return 157 * the ID of lcoreid's physical socket 158 */ 159 unsigned int 160 rte_lcore_to_socket_id(unsigned int lcore_id); 161 162 /** 163 * @warning 164 * @b EXPERIMENTAL: this API may change without prior notice. 165 * 166 * Return the id of the lcore on a socket starting from zero. 167 * 168 * @param lcore_id 169 * The targeted lcore, or -1 for the current one. 170 * @return 171 * The relative index, or -1 if not enabled. 172 */ 173 __rte_experimental 174 int 175 rte_lcore_to_cpu_id(int lcore_id); 176 177 #ifdef RTE_HAS_CPUSET 178 179 /** 180 * @warning 181 * @b EXPERIMENTAL: this API may change without prior notice. 182 * 183 * Return the cpuset for a given lcore. 184 * @param lcore_id 185 * the targeted lcore, which MUST be between 0 and RTE_MAX_LCORE-1. 186 * @return 187 * The cpuset of that lcore 188 */ 189 __rte_experimental 190 rte_cpuset_t 191 rte_lcore_cpuset(unsigned int lcore_id); 192 193 #endif /* RTE_HAS_CPUSET */ 194 195 /** 196 * Test if an lcore is enabled. 197 * 198 * @param lcore_id 199 * The identifier of the lcore, which MUST be between 0 and 200 * RTE_MAX_LCORE-1. 201 * @return 202 * True if the given lcore is enabled; false otherwise. 203 */ 204 int rte_lcore_is_enabled(unsigned int lcore_id); 205 206 /** 207 * Get the next enabled lcore ID. 208 * 209 * @param i 210 * The current lcore (reference). 211 * @param skip_main 212 * If true, do not return the ID of the main lcore. 213 * @param wrap 214 * If true, go back to 0 when RTE_MAX_LCORE is reached; otherwise, 215 * return RTE_MAX_LCORE. 216 * @return 217 * The next lcore_id or RTE_MAX_LCORE if not found. 218 */ 219 unsigned int rte_get_next_lcore(unsigned int i, int skip_main, int wrap); 220 221 /** 222 * Macro to browse all running lcores. 223 */ 224 #define RTE_LCORE_FOREACH(i) \ 225 for (i = rte_get_next_lcore(-1, 0, 0); \ 226 i < RTE_MAX_LCORE; \ 227 i = rte_get_next_lcore(i, 0, 0)) 228 229 /** 230 * Macro to browse all running lcores except the main lcore. 231 */ 232 #define RTE_LCORE_FOREACH_WORKER(i) \ 233 for (i = rte_get_next_lcore(-1, 1, 0); \ 234 i < RTE_MAX_LCORE; \ 235 i = rte_get_next_lcore(i, 1, 0)) 236 237 /** 238 * Callback prototype for initializing lcores. 239 * 240 * @param lcore_id 241 * The lcore to consider. 242 * @param arg 243 * An opaque pointer passed at callback registration. 244 * @return 245 * - -1 when refusing this operation, 246 * - 0 otherwise. 247 */ 248 typedef int (*rte_lcore_init_cb)(unsigned int lcore_id, void *arg); 249 250 /** 251 * Callback prototype for uninitializing lcores. 252 * 253 * @param lcore_id 254 * The lcore to consider. 255 * @param arg 256 * An opaque pointer passed at callback registration. 257 */ 258 typedef void (*rte_lcore_uninit_cb)(unsigned int lcore_id, void *arg); 259 260 /** 261 * Register callbacks invoked when initializing and uninitializing a lcore. 262 * 263 * This function calls the init callback with all initialized lcores. 264 * Any error reported by the init callback triggers a rollback calling the 265 * uninit callback for each lcore. 266 * If this step succeeds, the callbacks are put in the lcore callbacks list 267 * that will get called for each lcore allocation/release. 268 * 269 * Note: callbacks execution is serialised under a write lock protecting the 270 * lcores and callbacks list. 271 * 272 * @param name 273 * A name serving as a small description for this callback. 274 * @param init 275 * The callback invoked when a lcore_id is initialized. 276 * init can be NULL. 277 * @param uninit 278 * The callback invoked when a lcore_id is uninitialized. 279 * uninit can be NULL. 280 * @param arg 281 * An optional argument that gets passed to the callback when it gets 282 * invoked. 283 * @return 284 * On success, returns an opaque pointer for the registered object. 285 * On failure (either memory allocation issue in the function itself or an 286 * error is returned by the init callback itself), returns NULL. 287 */ 288 __rte_experimental 289 void * 290 rte_lcore_callback_register(const char *name, rte_lcore_init_cb init, 291 rte_lcore_uninit_cb uninit, void *arg); 292 293 /** 294 * Unregister callbacks previously registered with rte_lcore_callback_register. 295 * 296 * This function calls the uninit callback with all initialized lcores. 297 * The callbacks are then removed from the lcore callbacks list. 298 * 299 * @param handle 300 * The handle pointer returned by a former successful call to 301 * rte_lcore_callback_register. 302 */ 303 __rte_experimental 304 void 305 rte_lcore_callback_unregister(void *handle); 306 307 /** 308 * Callback prototype for iterating over lcores. 309 * 310 * @param lcore_id 311 * The lcore to consider. 312 * @param arg 313 * An opaque pointer coming from the caller. 314 * @return 315 * - 0 lets the iteration continue. 316 * - !0 makes the iteration stop. 317 */ 318 typedef int (*rte_lcore_iterate_cb)(unsigned int lcore_id, void *arg); 319 320 /** 321 * Iterate on all active lcores (ROLE_RTE, ROLE_SERVICE and ROLE_NON_EAL). 322 * No modification on the lcore states is allowed in the callback. 323 * 324 * Note: as opposed to init/uninit callbacks, iteration callbacks can be 325 * invoked in parallel as they are run under a read lock protecting the lcores 326 * and callbacks list. 327 * 328 * @param cb 329 * The callback that gets passed each lcore. 330 * @param arg 331 * An opaque pointer passed to cb. 332 * @return 333 * Same return code as the callback last invocation (see rte_lcore_iterate_cb 334 * description). 335 */ 336 __rte_experimental 337 int 338 rte_lcore_iterate(rte_lcore_iterate_cb cb, void *arg); 339 340 /** 341 * List all lcores. 342 * 343 * @param f 344 * The output stream where the dump should be sent. 345 */ 346 __rte_experimental 347 void 348 rte_lcore_dump(FILE *f); 349 350 /** 351 * Set thread names. 352 * 353 * @note It fails with glibc < 2.12. 354 * 355 * @param id 356 * Thread id. 357 * @param name 358 * Thread name to set. 359 * @return 360 * On success, return 0; otherwise return a negative value. 361 */ 362 int rte_thread_setname(pthread_t id, const char *name); 363 364 /** 365 * Get thread name. 366 * 367 * @note It fails with glibc < 2.12. 368 * 369 * @param id 370 * Thread id. 371 * @param name 372 * Thread name to set. 373 * @param len 374 * Thread name buffer length. 375 * @return 376 * On success, return 0; otherwise return a negative value. 377 */ 378 __rte_experimental 379 int rte_thread_getname(pthread_t id, char *name, size_t len); 380 381 /** 382 * Register current non-EAL thread as a lcore. 383 * 384 * @note This API is not compatible with the multi-process feature: 385 * - if a primary process registers a non-EAL thread, then no secondary process 386 * will initialise. 387 * - if a secondary process initialises successfully, trying to register a 388 * non-EAL thread from either primary or secondary processes will always end 389 * up with the thread getting LCORE_ID_ANY as lcore. 390 * 391 * @return 392 * On success, return 0; otherwise return -1 with rte_errno set. 393 */ 394 __rte_experimental 395 int 396 rte_thread_register(void); 397 398 /** 399 * Unregister current thread and release lcore if one was associated. 400 */ 401 __rte_experimental 402 void 403 rte_thread_unregister(void); 404 405 /** 406 * Create a control thread. 407 * 408 * Wrapper to pthread_create(), pthread_setname_np() and 409 * pthread_setaffinity_np(). The affinity of the new thread is based 410 * on the CPU affinity retrieved at the time rte_eal_init() was called, 411 * the dataplane and service lcores are then excluded. 412 * 413 * @param thread 414 * Filled with the thread id of the new created thread. 415 * @param name 416 * The name of the control thread (max 16 characters including '\0'). 417 * @param attr 418 * Attributes for the new thread. 419 * @param start_routine 420 * Function to be executed by the new thread. 421 * @param arg 422 * Argument passed to start_routine. 423 * @return 424 * On success, returns 0; on error, it returns a negative value 425 * corresponding to the error number. 426 */ 427 int 428 rte_ctrl_thread_create(pthread_t *thread, const char *name, 429 const pthread_attr_t *attr, 430 void *(*start_routine)(void *), void *arg); 431 432 #ifdef __cplusplus 433 } 434 #endif 435 436 437 #endif /* _RTE_LCORE_H_ */ 438