199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson */
499a2dd95SBruce Richardson
551e6608aSTyler Retzlaff #include <errno.h>
699a2dd95SBruce Richardson #include <stdio.h>
799a2dd95SBruce Richardson #include <stdlib.h>
899a2dd95SBruce Richardson #include <pthread.h>
999a2dd95SBruce Richardson #include <sched.h>
1099a2dd95SBruce Richardson #include <assert.h>
1199a2dd95SBruce Richardson #include <string.h>
1299a2dd95SBruce Richardson
1345a685caSAnkur Dwivedi #include <eal_trace_internal.h>
1499a2dd95SBruce Richardson #include <rte_errno.h>
1599a2dd95SBruce Richardson #include <rte_lcore.h>
1699a2dd95SBruce Richardson #include <rte_log.h>
1799a2dd95SBruce Richardson #include <rte_memory.h>
1899a2dd95SBruce Richardson #include <rte_trace_point.h>
1999a2dd95SBruce Richardson
2099a2dd95SBruce Richardson #include "eal_internal_cfg.h"
2199a2dd95SBruce Richardson #include "eal_private.h"
2299a2dd95SBruce Richardson #include "eal_thread.h"
2399a2dd95SBruce Richardson #include "eal_trace.h"
2499a2dd95SBruce Richardson
2599a2dd95SBruce Richardson RTE_DEFINE_PER_LCORE(unsigned int, _lcore_id) = LCORE_ID_ANY;
2699a2dd95SBruce Richardson RTE_DEFINE_PER_LCORE(int, _thread_id) = -1;
2799a2dd95SBruce Richardson static RTE_DEFINE_PER_LCORE(unsigned int, _socket_id) =
2899a2dd95SBruce Richardson (unsigned int)SOCKET_ID_ANY;
2999a2dd95SBruce Richardson static RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
3099a2dd95SBruce Richardson
rte_socket_id(void)3199a2dd95SBruce Richardson unsigned rte_socket_id(void)
3299a2dd95SBruce Richardson {
3399a2dd95SBruce Richardson return RTE_PER_LCORE(_socket_id);
3499a2dd95SBruce Richardson }
3599a2dd95SBruce Richardson
3699a2dd95SBruce Richardson static int
eal_cpuset_socket_id(rte_cpuset_t * cpusetp)3799a2dd95SBruce Richardson eal_cpuset_socket_id(rte_cpuset_t *cpusetp)
3899a2dd95SBruce Richardson {
3999a2dd95SBruce Richardson unsigned cpu = 0;
4099a2dd95SBruce Richardson int socket_id = SOCKET_ID_ANY;
4199a2dd95SBruce Richardson int sid;
4299a2dd95SBruce Richardson
4399a2dd95SBruce Richardson if (cpusetp == NULL)
4499a2dd95SBruce Richardson return SOCKET_ID_ANY;
4599a2dd95SBruce Richardson
4699a2dd95SBruce Richardson do {
4799a2dd95SBruce Richardson if (!CPU_ISSET(cpu, cpusetp))
4899a2dd95SBruce Richardson continue;
4999a2dd95SBruce Richardson
5099a2dd95SBruce Richardson if (socket_id == SOCKET_ID_ANY)
5199a2dd95SBruce Richardson socket_id = eal_cpu_socket_id(cpu);
5299a2dd95SBruce Richardson
5399a2dd95SBruce Richardson sid = eal_cpu_socket_id(cpu);
5499a2dd95SBruce Richardson if (socket_id != sid) {
5599a2dd95SBruce Richardson socket_id = SOCKET_ID_ANY;
5699a2dd95SBruce Richardson break;
5799a2dd95SBruce Richardson }
5899a2dd95SBruce Richardson
5999a2dd95SBruce Richardson } while (++cpu < CPU_SETSIZE);
6099a2dd95SBruce Richardson
6199a2dd95SBruce Richardson return socket_id;
6299a2dd95SBruce Richardson }
6399a2dd95SBruce Richardson
6499a2dd95SBruce Richardson static void
thread_update_affinity(rte_cpuset_t * cpusetp)6599a2dd95SBruce Richardson thread_update_affinity(rte_cpuset_t *cpusetp)
6699a2dd95SBruce Richardson {
6799a2dd95SBruce Richardson unsigned int lcore_id = rte_lcore_id();
6899a2dd95SBruce Richardson
6999a2dd95SBruce Richardson /* store socket_id in TLS for quick access */
7099a2dd95SBruce Richardson RTE_PER_LCORE(_socket_id) =
7199a2dd95SBruce Richardson eal_cpuset_socket_id(cpusetp);
7299a2dd95SBruce Richardson
7399a2dd95SBruce Richardson /* store cpuset in TLS for quick access */
7499a2dd95SBruce Richardson memmove(&RTE_PER_LCORE(_cpuset), cpusetp,
7599a2dd95SBruce Richardson sizeof(rte_cpuset_t));
7699a2dd95SBruce Richardson
7799a2dd95SBruce Richardson if (lcore_id != (unsigned)LCORE_ID_ANY) {
7899a2dd95SBruce Richardson /* EAL thread will update lcore_config */
7999a2dd95SBruce Richardson lcore_config[lcore_id].socket_id = RTE_PER_LCORE(_socket_id);
8099a2dd95SBruce Richardson memmove(&lcore_config[lcore_id].cpuset, cpusetp,
8199a2dd95SBruce Richardson sizeof(rte_cpuset_t));
8299a2dd95SBruce Richardson }
8399a2dd95SBruce Richardson }
8499a2dd95SBruce Richardson
8599a2dd95SBruce Richardson int
rte_thread_set_affinity(rte_cpuset_t * cpusetp)8699a2dd95SBruce Richardson rte_thread_set_affinity(rte_cpuset_t *cpusetp)
8799a2dd95SBruce Richardson {
888b0a1b8cSTyler Retzlaff if (rte_thread_set_affinity_by_id(rte_thread_self(), cpusetp) != 0) {
89*ae67895bSDavid Marchand EAL_LOG(ERR, "rte_thread_set_affinity_by_id failed");
9099a2dd95SBruce Richardson return -1;
9199a2dd95SBruce Richardson }
9299a2dd95SBruce Richardson
9399a2dd95SBruce Richardson thread_update_affinity(cpusetp);
9499a2dd95SBruce Richardson return 0;
9599a2dd95SBruce Richardson }
9699a2dd95SBruce Richardson
9799a2dd95SBruce Richardson void
rte_thread_get_affinity(rte_cpuset_t * cpusetp)9899a2dd95SBruce Richardson rte_thread_get_affinity(rte_cpuset_t *cpusetp)
9999a2dd95SBruce Richardson {
10099a2dd95SBruce Richardson assert(cpusetp);
10199a2dd95SBruce Richardson memmove(cpusetp, &RTE_PER_LCORE(_cpuset),
10299a2dd95SBruce Richardson sizeof(rte_cpuset_t));
10399a2dd95SBruce Richardson }
10499a2dd95SBruce Richardson
10599a2dd95SBruce Richardson int
eal_thread_dump_affinity(rte_cpuset_t * cpuset,char * str,unsigned int size)10699a2dd95SBruce Richardson eal_thread_dump_affinity(rte_cpuset_t *cpuset, char *str, unsigned int size)
10799a2dd95SBruce Richardson {
10899a2dd95SBruce Richardson unsigned cpu;
10999a2dd95SBruce Richardson int ret;
11099a2dd95SBruce Richardson unsigned int out = 0;
11199a2dd95SBruce Richardson
11299a2dd95SBruce Richardson for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
11399a2dd95SBruce Richardson if (!CPU_ISSET(cpu, cpuset))
11499a2dd95SBruce Richardson continue;
11599a2dd95SBruce Richardson
11699a2dd95SBruce Richardson ret = snprintf(str + out,
11799a2dd95SBruce Richardson size - out, "%u,", cpu);
11899a2dd95SBruce Richardson if (ret < 0 || (unsigned)ret >= size - out) {
11999a2dd95SBruce Richardson /* string will be truncated */
12099a2dd95SBruce Richardson ret = -1;
12199a2dd95SBruce Richardson goto exit;
12299a2dd95SBruce Richardson }
12399a2dd95SBruce Richardson
12499a2dd95SBruce Richardson out += ret;
12599a2dd95SBruce Richardson }
12699a2dd95SBruce Richardson
12799a2dd95SBruce Richardson ret = 0;
12899a2dd95SBruce Richardson exit:
12999a2dd95SBruce Richardson /* remove the last separator */
13099a2dd95SBruce Richardson if (out > 0)
13199a2dd95SBruce Richardson str[out - 1] = '\0';
13299a2dd95SBruce Richardson
13399a2dd95SBruce Richardson return ret;
13499a2dd95SBruce Richardson }
13599a2dd95SBruce Richardson
13699a2dd95SBruce Richardson int
eal_thread_dump_current_affinity(char * str,unsigned int size)13799a2dd95SBruce Richardson eal_thread_dump_current_affinity(char *str, unsigned int size)
13899a2dd95SBruce Richardson {
13999a2dd95SBruce Richardson rte_cpuset_t cpuset;
14099a2dd95SBruce Richardson
14199a2dd95SBruce Richardson rte_thread_get_affinity(&cpuset);
14299a2dd95SBruce Richardson return eal_thread_dump_affinity(&cpuset, str, size);
14399a2dd95SBruce Richardson }
14499a2dd95SBruce Richardson
14599a2dd95SBruce Richardson void
__rte_thread_init(unsigned int lcore_id,rte_cpuset_t * cpuset)14699a2dd95SBruce Richardson __rte_thread_init(unsigned int lcore_id, rte_cpuset_t *cpuset)
14799a2dd95SBruce Richardson {
14899a2dd95SBruce Richardson /* set the lcore ID in per-lcore memory area */
14999a2dd95SBruce Richardson RTE_PER_LCORE(_lcore_id) = lcore_id;
15099a2dd95SBruce Richardson
15199a2dd95SBruce Richardson /* acquire system unique id */
15299a2dd95SBruce Richardson rte_gettid();
15399a2dd95SBruce Richardson
15499a2dd95SBruce Richardson thread_update_affinity(cpuset);
15599a2dd95SBruce Richardson
15699a2dd95SBruce Richardson __rte_trace_mem_per_thread_alloc();
15799a2dd95SBruce Richardson }
15899a2dd95SBruce Richardson
15999a2dd95SBruce Richardson void
__rte_thread_uninit(void)16099a2dd95SBruce Richardson __rte_thread_uninit(void)
16199a2dd95SBruce Richardson {
16299a2dd95SBruce Richardson trace_mem_per_thread_free();
16399a2dd95SBruce Richardson
16499a2dd95SBruce Richardson RTE_PER_LCORE(_lcore_id) = LCORE_ID_ANY;
16599a2dd95SBruce Richardson }
16699a2dd95SBruce Richardson
167a95d7054SDavid Marchand /* main loop of threads */
1688b0a1b8cSTyler Retzlaff __rte_noreturn uint32_t
eal_thread_loop(void * arg)169a95d7054SDavid Marchand eal_thread_loop(void *arg)
170a95d7054SDavid Marchand {
171a95d7054SDavid Marchand unsigned int lcore_id = (uintptr_t)arg;
172a95d7054SDavid Marchand char cpuset[RTE_CPU_AFFINITY_STR_LEN];
173a95d7054SDavid Marchand int ret;
174a95d7054SDavid Marchand
175a95d7054SDavid Marchand __rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset);
176a95d7054SDavid Marchand
177a95d7054SDavid Marchand ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset));
178*ae67895bSDavid Marchand EAL_LOG(DEBUG, "lcore %u is ready (tid=%zx;cpuset=[%s%s])",
179db77fe7dSTyler Retzlaff lcore_id, rte_thread_self().opaque_id, cpuset,
180a95d7054SDavid Marchand ret == 0 ? "" : "...");
181a95d7054SDavid Marchand
182a95d7054SDavid Marchand rte_eal_trace_thread_lcore_ready(lcore_id, cpuset);
183a95d7054SDavid Marchand
184a95d7054SDavid Marchand /* read on our pipe to get commands */
185a95d7054SDavid Marchand while (1) {
186a95d7054SDavid Marchand lcore_function_t *f;
187a95d7054SDavid Marchand void *fct_arg;
188a95d7054SDavid Marchand
189a95d7054SDavid Marchand eal_thread_wait_command();
190a95d7054SDavid Marchand
191a95d7054SDavid Marchand /* Set the state to 'RUNNING'. Use release order
192a95d7054SDavid Marchand * since 'state' variable is used as the guard variable.
193a95d7054SDavid Marchand */
1942a7a42a5STyler Retzlaff rte_atomic_store_explicit(&lcore_config[lcore_id].state, RUNNING,
1952a7a42a5STyler Retzlaff rte_memory_order_release);
196a95d7054SDavid Marchand
197a95d7054SDavid Marchand eal_thread_ack_command();
198a95d7054SDavid Marchand
199a95d7054SDavid Marchand /* Load 'f' with acquire order to ensure that
200a95d7054SDavid Marchand * the memory operations from the main thread
201a95d7054SDavid Marchand * are accessed only after update to 'f' is visible.
202a95d7054SDavid Marchand * Wait till the update to 'f' is visible to the worker.
203a95d7054SDavid Marchand */
2042a7a42a5STyler Retzlaff while ((f = rte_atomic_load_explicit(&lcore_config[lcore_id].f,
2052a7a42a5STyler Retzlaff rte_memory_order_acquire)) == NULL)
206a95d7054SDavid Marchand rte_pause();
207a95d7054SDavid Marchand
208841e87dfSArnaud Fiorini rte_eal_trace_thread_lcore_running(lcore_id, f);
209841e87dfSArnaud Fiorini
210a95d7054SDavid Marchand /* call the function and store the return value */
211a95d7054SDavid Marchand fct_arg = lcore_config[lcore_id].arg;
212a95d7054SDavid Marchand ret = f(fct_arg);
213a95d7054SDavid Marchand lcore_config[lcore_id].ret = ret;
214a95d7054SDavid Marchand lcore_config[lcore_id].f = NULL;
215a95d7054SDavid Marchand lcore_config[lcore_id].arg = NULL;
216a95d7054SDavid Marchand
217a95d7054SDavid Marchand /* Store the state with release order to ensure that
218a95d7054SDavid Marchand * the memory operations from the worker thread
219a95d7054SDavid Marchand * are completed before the state is updated.
220a95d7054SDavid Marchand * Use 'state' as the guard variable.
221a95d7054SDavid Marchand */
2222a7a42a5STyler Retzlaff rte_atomic_store_explicit(&lcore_config[lcore_id].state, WAIT,
2232a7a42a5STyler Retzlaff rte_memory_order_release);
224841e87dfSArnaud Fiorini
225841e87dfSArnaud Fiorini rte_eal_trace_thread_lcore_stopped(lcore_id);
226a95d7054SDavid Marchand }
227a95d7054SDavid Marchand
228a95d7054SDavid Marchand /* never reached */
2298b0a1b8cSTyler Retzlaff /* return 0; */
230a95d7054SDavid Marchand }
231a95d7054SDavid Marchand
232705356f0SHonnappa Nagarahalli enum __rte_ctrl_thread_status {
233705356f0SHonnappa Nagarahalli CTRL_THREAD_LAUNCHING, /* Yet to call pthread_create function */
234705356f0SHonnappa Nagarahalli CTRL_THREAD_RUNNING, /* Control thread is running successfully */
235705356f0SHonnappa Nagarahalli CTRL_THREAD_ERROR /* Control thread encountered an error */
236705356f0SHonnappa Nagarahalli };
237705356f0SHonnappa Nagarahalli
23857ecf148SThomas Monjalon struct control_thread_params {
23957ecf148SThomas Monjalon rte_thread_func start_routine;
24099a2dd95SBruce Richardson void *arg;
241705356f0SHonnappa Nagarahalli int ret;
242705356f0SHonnappa Nagarahalli /* Control thread status.
243705356f0SHonnappa Nagarahalli * If the status is CTRL_THREAD_ERROR, 'ret' has the error code.
244705356f0SHonnappa Nagarahalli */
2452a7a42a5STyler Retzlaff RTE_ATOMIC(enum __rte_ctrl_thread_status) status;
24699a2dd95SBruce Richardson };
24799a2dd95SBruce Richardson
control_thread_init(void * arg)24857ecf148SThomas Monjalon static int control_thread_init(void *arg)
24999a2dd95SBruce Richardson {
25099a2dd95SBruce Richardson struct internal_config *internal_conf =
25199a2dd95SBruce Richardson eal_get_internal_configuration();
25299a2dd95SBruce Richardson rte_cpuset_t *cpuset = &internal_conf->ctrl_cpuset;
25357ecf148SThomas Monjalon struct control_thread_params *params = arg;
25499a2dd95SBruce Richardson
25599a2dd95SBruce Richardson __rte_thread_init(rte_lcore_id(), cpuset);
2567e7b6762SKaisen You /* Set control thread socket ID to SOCKET_ID_ANY
2577e7b6762SKaisen You * as control threads may be scheduled on any NUMA node.
2587e7b6762SKaisen You */
2597e7b6762SKaisen You RTE_PER_LCORE(_socket_id) = SOCKET_ID_ANY;
2608b0a1b8cSTyler Retzlaff params->ret = rte_thread_set_affinity_by_id(rte_thread_self(), cpuset);
261705356f0SHonnappa Nagarahalli if (params->ret != 0) {
2622a7a42a5STyler Retzlaff rte_atomic_store_explicit(¶ms->status,
2632a7a42a5STyler Retzlaff CTRL_THREAD_ERROR, rte_memory_order_release);
264b27c1bd8STyler Retzlaff return 1;
265705356f0SHonnappa Nagarahalli }
266705356f0SHonnappa Nagarahalli
2672a7a42a5STyler Retzlaff rte_atomic_store_explicit(¶ms->status,
2682a7a42a5STyler Retzlaff CTRL_THREAD_RUNNING, rte_memory_order_release);
26999a2dd95SBruce Richardson
270878b7468STyler Retzlaff return 0;
271878b7468STyler Retzlaff }
272878b7468STyler Retzlaff
control_thread_start(void * arg)273878b7468STyler Retzlaff static uint32_t control_thread_start(void *arg)
274878b7468STyler Retzlaff {
27557ecf148SThomas Monjalon struct control_thread_params *params = arg;
276b27c1bd8STyler Retzlaff void *start_arg = params->arg;
27757ecf148SThomas Monjalon rte_thread_func start_routine = params->start_routine;
278878b7468STyler Retzlaff
27957ecf148SThomas Monjalon if (control_thread_init(arg) != 0)
280b27c1bd8STyler Retzlaff return 0;
281878b7468STyler Retzlaff
282b27c1bd8STyler Retzlaff return start_routine(start_arg);
28399a2dd95SBruce Richardson }
28499a2dd95SBruce Richardson
28599a2dd95SBruce Richardson int
rte_thread_create_control(rte_thread_t * thread,const char * name,rte_thread_func start_routine,void * arg)286878b7468STyler Retzlaff rte_thread_create_control(rte_thread_t *thread, const char *name,
28770d19787SThomas Monjalon rte_thread_func start_routine, void *arg)
288878b7468STyler Retzlaff {
28957ecf148SThomas Monjalon struct control_thread_params *params;
290878b7468STyler Retzlaff enum __rte_ctrl_thread_status ctrl_thread_status;
291878b7468STyler Retzlaff int ret;
292878b7468STyler Retzlaff
293878b7468STyler Retzlaff params = malloc(sizeof(*params));
294878b7468STyler Retzlaff if (params == NULL)
295878b7468STyler Retzlaff return -ENOMEM;
296878b7468STyler Retzlaff
29757ecf148SThomas Monjalon params->start_routine = start_routine;
298878b7468STyler Retzlaff params->arg = arg;
299878b7468STyler Retzlaff params->ret = 0;
30057ecf148SThomas Monjalon params->status = CTRL_THREAD_LAUNCHING;
301878b7468STyler Retzlaff
30270d19787SThomas Monjalon ret = rte_thread_create(thread, NULL, control_thread_start, params);
303878b7468STyler Retzlaff if (ret != 0) {
304878b7468STyler Retzlaff free(params);
305878b7468STyler Retzlaff return -ret;
306878b7468STyler Retzlaff }
307878b7468STyler Retzlaff
308878b7468STyler Retzlaff if (name != NULL)
309878b7468STyler Retzlaff rte_thread_set_name(*thread, name);
310878b7468STyler Retzlaff
311878b7468STyler Retzlaff /* Wait for the control thread to initialize successfully */
312878b7468STyler Retzlaff while ((ctrl_thread_status =
3132a7a42a5STyler Retzlaff rte_atomic_load_explicit(¶ms->status,
3142a7a42a5STyler Retzlaff rte_memory_order_acquire)) == CTRL_THREAD_LAUNCHING) {
315878b7468STyler Retzlaff rte_delay_us_sleep(1);
316878b7468STyler Retzlaff }
317878b7468STyler Retzlaff
318878b7468STyler Retzlaff /* Check if the control thread encountered an error */
319878b7468STyler Retzlaff if (ctrl_thread_status == CTRL_THREAD_ERROR) {
320878b7468STyler Retzlaff /* ctrl thread is exiting */
321878b7468STyler Retzlaff rte_thread_join(*thread, NULL);
322878b7468STyler Retzlaff }
323878b7468STyler Retzlaff
324878b7468STyler Retzlaff ret = params->ret;
325878b7468STyler Retzlaff free(params);
326878b7468STyler Retzlaff
327878b7468STyler Retzlaff return ret;
328878b7468STyler Retzlaff }
329878b7468STyler Retzlaff
330ce703c47SThomas Monjalon static void
add_internal_prefix(char * prefixed_name,const char * name,size_t size)331ce703c47SThomas Monjalon add_internal_prefix(char *prefixed_name, const char *name, size_t size)
332ce703c47SThomas Monjalon {
333ce703c47SThomas Monjalon size_t prefixlen;
334ce703c47SThomas Monjalon
335ce703c47SThomas Monjalon /* Check RTE_THREAD_INTERNAL_NAME_SIZE definition. */
336ce703c47SThomas Monjalon RTE_BUILD_BUG_ON(RTE_THREAD_INTERNAL_NAME_SIZE !=
337ce703c47SThomas Monjalon RTE_THREAD_NAME_SIZE - sizeof(RTE_THREAD_INTERNAL_PREFIX) + 1);
338ce703c47SThomas Monjalon
339ce703c47SThomas Monjalon prefixlen = strlen(RTE_THREAD_INTERNAL_PREFIX);
340ce703c47SThomas Monjalon strlcpy(prefixed_name, RTE_THREAD_INTERNAL_PREFIX, size);
341ce703c47SThomas Monjalon strlcpy(prefixed_name + prefixlen, name, size - prefixlen);
342ce703c47SThomas Monjalon }
343ce703c47SThomas Monjalon
344ce703c47SThomas Monjalon int
rte_thread_create_internal_control(rte_thread_t * id,const char * name,rte_thread_func func,void * arg)345ce703c47SThomas Monjalon rte_thread_create_internal_control(rte_thread_t *id, const char *name,
346ce703c47SThomas Monjalon rte_thread_func func, void *arg)
347ce703c47SThomas Monjalon {
348ce703c47SThomas Monjalon char prefixed_name[RTE_THREAD_NAME_SIZE];
349ce703c47SThomas Monjalon
350ce703c47SThomas Monjalon add_internal_prefix(prefixed_name, name, sizeof(prefixed_name));
351ce703c47SThomas Monjalon return rte_thread_create_control(id, prefixed_name, func, arg);
352ce703c47SThomas Monjalon }
353ce703c47SThomas Monjalon
354ce703c47SThomas Monjalon void
rte_thread_set_prefixed_name(rte_thread_t id,const char * name)355ce703c47SThomas Monjalon rte_thread_set_prefixed_name(rte_thread_t id, const char *name)
356ce703c47SThomas Monjalon {
357ce703c47SThomas Monjalon char prefixed_name[RTE_THREAD_NAME_SIZE];
358ce703c47SThomas Monjalon
359ce703c47SThomas Monjalon add_internal_prefix(prefixed_name, name, sizeof(prefixed_name));
360ce703c47SThomas Monjalon rte_thread_set_name(id, prefixed_name);
361ce703c47SThomas Monjalon }
362ce703c47SThomas Monjalon
363878b7468STyler Retzlaff int
rte_thread_register(void)36499a2dd95SBruce Richardson rte_thread_register(void)
36599a2dd95SBruce Richardson {
36699a2dd95SBruce Richardson unsigned int lcore_id;
36799a2dd95SBruce Richardson rte_cpuset_t cpuset;
36899a2dd95SBruce Richardson
36999a2dd95SBruce Richardson /* EAL init flushes all lcores, we can't register before. */
37099a2dd95SBruce Richardson if (eal_get_internal_configuration()->init_complete != 1) {
371*ae67895bSDavid Marchand EAL_LOG(DEBUG, "Called %s before EAL init.", __func__);
37299a2dd95SBruce Richardson rte_errno = EINVAL;
37399a2dd95SBruce Richardson return -1;
37499a2dd95SBruce Richardson }
37599a2dd95SBruce Richardson if (!rte_mp_disable()) {
376*ae67895bSDavid Marchand EAL_LOG(ERR, "Multiprocess in use, registering non-EAL threads is not supported.");
37799a2dd95SBruce Richardson rte_errno = EINVAL;
37899a2dd95SBruce Richardson return -1;
37999a2dd95SBruce Richardson }
3808b0a1b8cSTyler Retzlaff if (rte_thread_get_affinity_by_id(rte_thread_self(), &cpuset) != 0)
38199a2dd95SBruce Richardson CPU_ZERO(&cpuset);
38299a2dd95SBruce Richardson lcore_id = eal_lcore_non_eal_allocate();
38399a2dd95SBruce Richardson if (lcore_id >= RTE_MAX_LCORE)
38499a2dd95SBruce Richardson lcore_id = LCORE_ID_ANY;
38599a2dd95SBruce Richardson __rte_thread_init(lcore_id, &cpuset);
38699a2dd95SBruce Richardson if (lcore_id == LCORE_ID_ANY) {
38799a2dd95SBruce Richardson rte_errno = ENOMEM;
38899a2dd95SBruce Richardson return -1;
38999a2dd95SBruce Richardson }
390*ae67895bSDavid Marchand EAL_LOG(DEBUG, "Registered non-EAL thread as lcore %u.",
39199a2dd95SBruce Richardson lcore_id);
39299a2dd95SBruce Richardson return 0;
39399a2dd95SBruce Richardson }
39499a2dd95SBruce Richardson
39599a2dd95SBruce Richardson void
rte_thread_unregister(void)39699a2dd95SBruce Richardson rte_thread_unregister(void)
39799a2dd95SBruce Richardson {
39899a2dd95SBruce Richardson unsigned int lcore_id = rte_lcore_id();
39999a2dd95SBruce Richardson
40099a2dd95SBruce Richardson if (lcore_id != LCORE_ID_ANY)
40199a2dd95SBruce Richardson eal_lcore_non_eal_release(lcore_id);
40299a2dd95SBruce Richardson __rte_thread_uninit();
40399a2dd95SBruce Richardson if (lcore_id != LCORE_ID_ANY)
404*ae67895bSDavid Marchand EAL_LOG(DEBUG, "Unregistered non-EAL thread (was lcore %u).",
40599a2dd95SBruce Richardson lcore_id);
40699a2dd95SBruce Richardson }
40751e6608aSTyler Retzlaff
40851e6608aSTyler Retzlaff int
rte_thread_attr_init(rte_thread_attr_t * attr)40951e6608aSTyler Retzlaff rte_thread_attr_init(rte_thread_attr_t *attr)
41051e6608aSTyler Retzlaff {
41151e6608aSTyler Retzlaff if (attr == NULL)
41251e6608aSTyler Retzlaff return EINVAL;
41351e6608aSTyler Retzlaff
41451e6608aSTyler Retzlaff CPU_ZERO(&attr->cpuset);
41551e6608aSTyler Retzlaff attr->priority = RTE_THREAD_PRIORITY_NORMAL;
41651e6608aSTyler Retzlaff
41751e6608aSTyler Retzlaff return 0;
41851e6608aSTyler Retzlaff }
41951e6608aSTyler Retzlaff
42051e6608aSTyler Retzlaff int
rte_thread_attr_set_priority(rte_thread_attr_t * thread_attr,enum rte_thread_priority priority)42151e6608aSTyler Retzlaff rte_thread_attr_set_priority(rte_thread_attr_t *thread_attr,
42251e6608aSTyler Retzlaff enum rte_thread_priority priority)
42351e6608aSTyler Retzlaff {
42451e6608aSTyler Retzlaff if (thread_attr == NULL)
42551e6608aSTyler Retzlaff return EINVAL;
42651e6608aSTyler Retzlaff
42751e6608aSTyler Retzlaff thread_attr->priority = priority;
42851e6608aSTyler Retzlaff
42951e6608aSTyler Retzlaff return 0;
43051e6608aSTyler Retzlaff }
43151e6608aSTyler Retzlaff
43251e6608aSTyler Retzlaff int
rte_thread_attr_set_affinity(rte_thread_attr_t * thread_attr,rte_cpuset_t * cpuset)43351e6608aSTyler Retzlaff rte_thread_attr_set_affinity(rte_thread_attr_t *thread_attr,
43451e6608aSTyler Retzlaff rte_cpuset_t *cpuset)
43551e6608aSTyler Retzlaff {
43651e6608aSTyler Retzlaff if (thread_attr == NULL)
43751e6608aSTyler Retzlaff return EINVAL;
43851e6608aSTyler Retzlaff
43951e6608aSTyler Retzlaff if (cpuset == NULL)
44051e6608aSTyler Retzlaff return EINVAL;
44151e6608aSTyler Retzlaff
44251e6608aSTyler Retzlaff thread_attr->cpuset = *cpuset;
44351e6608aSTyler Retzlaff
44451e6608aSTyler Retzlaff return 0;
44551e6608aSTyler Retzlaff }
44651e6608aSTyler Retzlaff
44751e6608aSTyler Retzlaff int
rte_thread_attr_get_affinity(rte_thread_attr_t * thread_attr,rte_cpuset_t * cpuset)44851e6608aSTyler Retzlaff rte_thread_attr_get_affinity(rte_thread_attr_t *thread_attr,
44951e6608aSTyler Retzlaff rte_cpuset_t *cpuset)
45051e6608aSTyler Retzlaff {
45151e6608aSTyler Retzlaff if (thread_attr == NULL)
45251e6608aSTyler Retzlaff return EINVAL;
45351e6608aSTyler Retzlaff
45451e6608aSTyler Retzlaff if (cpuset == NULL)
45551e6608aSTyler Retzlaff return EINVAL;
45651e6608aSTyler Retzlaff
45751e6608aSTyler Retzlaff *cpuset = thread_attr->cpuset;
45851e6608aSTyler Retzlaff
45951e6608aSTyler Retzlaff return 0;
46051e6608aSTyler Retzlaff }
461