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 <sched.h> 11 #include <pthread_np.h> 12 #include <sys/queue.h> 13 #include <sys/thr.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 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=%p;cpuset=[%s%s])\n", 97 lcore_id, 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 lcore_config[lcore_id].state = FINISHED; 131 } 132 133 /* never reached */ 134 /* pthread_exit(NULL); */ 135 /* return NULL; */ 136 } 137 138 /* require calling thread tid by gettid() */ 139 int rte_sys_gettid(void) 140 { 141 long lwpid; 142 thr_self(&lwpid); 143 return (int)lwpid; 144 } 145 146 int rte_thread_setname(pthread_t id, const char *name) 147 { 148 /* this BSD function returns no error */ 149 pthread_set_name_np(id, name); 150 return 0; 151 } 152 153 int rte_thread_getname(pthread_t id, char *name, size_t len) 154 { 155 RTE_SET_USED(id); 156 RTE_SET_USED(name); 157 RTE_SET_USED(len); 158 159 return -ENOTSUP; 160 } 161