1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 5 #include <errno.h> 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <stdint.h> 9 #include <unistd.h> 10 #include <pthread.h> 11 #include <sched.h> 12 #include <sys/queue.h> 13 #include <sys/syscall.h> 14 15 #include <rte_debug.h> 16 #include <rte_atomic.h> 17 #include <rte_launch.h> 18 #include <rte_log.h> 19 #include <rte_memory.h> 20 #include <rte_per_lcore.h> 21 #include <rte_eal.h> 22 #include <rte_lcore.h> 23 #include <rte_eal_trace.h> 24 25 #include "eal_private.h" 26 #include "eal_thread.h" 27 28 /* 29 * Send a message to a worker lcore identified by worker_id to call a 30 * function f with argument arg. Once the execution is done, the 31 * remote lcore switch in FINISHED state. 32 */ 33 int 34 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned int worker_id) 35 { 36 int n; 37 char c = 0; 38 int m2w = lcore_config[worker_id].pipe_main2worker[1]; 39 int w2m = lcore_config[worker_id].pipe_worker2main[0]; 40 int rc = -EBUSY; 41 42 if (lcore_config[worker_id].state != WAIT) 43 goto finish; 44 45 lcore_config[worker_id].f = f; 46 lcore_config[worker_id].arg = arg; 47 48 /* send message */ 49 n = 0; 50 while (n == 0 || (n < 0 && errno == EINTR)) 51 n = write(m2w, &c, 1); 52 if (n < 0) 53 rte_panic("cannot write on configuration pipe\n"); 54 55 /* wait ack */ 56 do { 57 n = read(w2m, &c, 1); 58 } while (n < 0 && errno == EINTR); 59 60 if (n <= 0) 61 rte_panic("cannot read on configuration pipe\n"); 62 63 rc = 0; 64 finish: 65 rte_eal_trace_thread_remote_launch(f, arg, worker_id, rc); 66 return rc; 67 } 68 69 /* main loop of threads */ 70 __rte_noreturn void * 71 eal_thread_loop(__rte_unused void *arg) 72 { 73 char c; 74 int n, ret; 75 unsigned lcore_id; 76 pthread_t thread_id; 77 int m2w, w2m; 78 char cpuset[RTE_CPU_AFFINITY_STR_LEN]; 79 80 thread_id = pthread_self(); 81 82 /* retrieve our lcore_id from the configuration structure */ 83 RTE_LCORE_FOREACH_WORKER(lcore_id) { 84 if (thread_id == lcore_config[lcore_id].thread_id) 85 break; 86 } 87 if (lcore_id == RTE_MAX_LCORE) 88 rte_panic("cannot retrieve lcore id\n"); 89 90 m2w = lcore_config[lcore_id].pipe_main2worker[0]; 91 w2m = lcore_config[lcore_id].pipe_worker2main[1]; 92 93 __rte_thread_init(lcore_id, &lcore_config[lcore_id].cpuset); 94 95 ret = eal_thread_dump_current_affinity(cpuset, sizeof(cpuset)); 96 RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s%s])\n", 97 lcore_id, (uintptr_t)thread_id, cpuset, ret == 0 ? "" : "..."); 98 99 rte_eal_trace_thread_lcore_ready(lcore_id, cpuset); 100 101 /* read on our pipe to get commands */ 102 while (1) { 103 void *fct_arg; 104 105 /* wait command */ 106 do { 107 n = read(m2w, &c, 1); 108 } while (n < 0 && errno == EINTR); 109 110 if (n <= 0) 111 rte_panic("cannot read on configuration pipe\n"); 112 113 lcore_config[lcore_id].state = RUNNING; 114 115 /* send ack */ 116 n = 0; 117 while (n == 0 || (n < 0 && errno == EINTR)) 118 n = write(w2m, &c, 1); 119 if (n < 0) 120 rte_panic("cannot write on configuration pipe\n"); 121 122 if (lcore_config[lcore_id].f == NULL) 123 rte_panic("NULL function pointer\n"); 124 125 /* call the function and store the return value */ 126 fct_arg = lcore_config[lcore_id].arg; 127 ret = lcore_config[lcore_id].f(fct_arg); 128 lcore_config[lcore_id].ret = ret; 129 rte_wmb(); 130 131 /* when a service core returns, it should go directly to WAIT 132 * state, because the application will not lcore_wait() for it. 133 */ 134 if (lcore_config[lcore_id].core_role == ROLE_SERVICE) 135 lcore_config[lcore_id].state = WAIT; 136 else 137 lcore_config[lcore_id].state = FINISHED; 138 } 139 140 /* never reached */ 141 /* pthread_exit(NULL); */ 142 /* return NULL; */ 143 } 144 145 /* require calling thread tid by gettid() */ 146 int rte_sys_gettid(void) 147 { 148 return (int)syscall(SYS_gettid); 149 } 150 151 int rte_thread_setname(pthread_t id, const char *name) 152 { 153 int ret = ENOSYS; 154 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ) 155 #if __GLIBC_PREREQ(2, 12) 156 char truncated[16]; 157 158 strlcpy(truncated, name, sizeof(truncated)); 159 ret = pthread_setname_np(id, truncated); 160 #endif 161 #endif 162 RTE_SET_USED(id); 163 RTE_SET_USED(name); 164 return -ret; 165 } 166 167 int rte_thread_getname(pthread_t id, char *name, size_t len) 168 { 169 int ret = ENOSYS; 170 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ) 171 #if __GLIBC_PREREQ(2, 12) 172 ret = pthread_getname_np(id, name, len); 173 #endif 174 #endif 175 RTE_SET_USED(id); 176 RTE_SET_USED(name); 177 RTE_SET_USED(len); 178 return -ret; 179 180 } 181