New upstream version 18.11-rc1
[deb_dpdk.git] / lib / librte_eal / linuxapp / eal / eal_thread.c
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
24 #include "eal_private.h"
25 #include "eal_thread.h"
26
27 RTE_DEFINE_PER_LCORE(unsigned, _lcore_id) = LCORE_ID_ANY;
28 RTE_DEFINE_PER_LCORE(unsigned, _socket_id) = (unsigned)SOCKET_ID_ANY;
29 RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
30
31 /*
32  * Send a message to a slave lcore identified by slave_id to call a
33  * function f with argument arg. Once the execution is done, the
34  * remote lcore switch in FINISHED state.
35  */
36 int
37 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
38 {
39         int n;
40         char c = 0;
41         int m2s = lcore_config[slave_id].pipe_master2slave[1];
42         int s2m = lcore_config[slave_id].pipe_slave2master[0];
43
44         if (lcore_config[slave_id].state != WAIT)
45                 return -EBUSY;
46
47         lcore_config[slave_id].f = f;
48         lcore_config[slave_id].arg = arg;
49
50         /* send message */
51         n = 0;
52         while (n == 0 || (n < 0 && errno == EINTR))
53                 n = write(m2s, &c, 1);
54         if (n < 0)
55                 rte_panic("cannot write on configuration pipe\n");
56
57         /* wait ack */
58         do {
59                 n = read(s2m, &c, 1);
60         } while (n < 0 && errno == EINTR);
61
62         if (n <= 0)
63                 rte_panic("cannot read on configuration pipe\n");
64
65         return 0;
66 }
67
68 /* set affinity for current EAL thread */
69 static int
70 eal_thread_set_affinity(void)
71 {
72         unsigned lcore_id = rte_lcore_id();
73
74         /* acquire system unique id  */
75         rte_gettid();
76
77         /* update EAL thread core affinity */
78         return rte_thread_set_affinity(&lcore_config[lcore_id].cpuset);
79 }
80
81 void eal_thread_init_master(unsigned lcore_id)
82 {
83         /* set the lcore ID in per-lcore memory area */
84         RTE_PER_LCORE(_lcore_id) = lcore_id;
85
86         /* set CPU affinity */
87         if (eal_thread_set_affinity() < 0)
88                 rte_panic("cannot set affinity\n");
89 }
90
91 /* main loop of threads */
92 __attribute__((noreturn)) void *
93 eal_thread_loop(__attribute__((unused)) void *arg)
94 {
95         char c;
96         int n, ret;
97         unsigned lcore_id;
98         pthread_t thread_id;
99         int m2s, s2m;
100         char cpuset[RTE_CPU_AFFINITY_STR_LEN];
101
102         thread_id = pthread_self();
103
104         /* retrieve our lcore_id from the configuration structure */
105         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
106                 if (thread_id == lcore_config[lcore_id].thread_id)
107                         break;
108         }
109         if (lcore_id == RTE_MAX_LCORE)
110                 rte_panic("cannot retrieve lcore id\n");
111
112         m2s = lcore_config[lcore_id].pipe_master2slave[0];
113         s2m = lcore_config[lcore_id].pipe_slave2master[1];
114
115         /* set the lcore ID in per-lcore memory area */
116         RTE_PER_LCORE(_lcore_id) = lcore_id;
117
118         /* set CPU affinity */
119         if (eal_thread_set_affinity() < 0)
120                 rte_panic("cannot set affinity\n");
121
122         ret = eal_thread_dump_affinity(cpuset, sizeof(cpuset));
123
124         RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%zx;cpuset=[%s%s])\n",
125                 lcore_id, (uintptr_t)thread_id, cpuset, ret == 0 ? "" : "...");
126
127         /* read on our pipe to get commands */
128         while (1) {
129                 void *fct_arg;
130
131                 /* wait command */
132                 do {
133                         n = read(m2s, &c, 1);
134                 } while (n < 0 && errno == EINTR);
135
136                 if (n <= 0)
137                         rte_panic("cannot read on configuration pipe\n");
138
139                 lcore_config[lcore_id].state = RUNNING;
140
141                 /* send ack */
142                 n = 0;
143                 while (n == 0 || (n < 0 && errno == EINTR))
144                         n = write(s2m, &c, 1);
145                 if (n < 0)
146                         rte_panic("cannot write on configuration pipe\n");
147
148                 if (lcore_config[lcore_id].f == NULL)
149                         rte_panic("NULL function pointer\n");
150
151                 /* call the function and store the return value */
152                 fct_arg = lcore_config[lcore_id].arg;
153                 ret = lcore_config[lcore_id].f(fct_arg);
154                 lcore_config[lcore_id].ret = ret;
155                 rte_wmb();
156
157                 /* when a service core returns, it should go directly to WAIT
158                  * state, because the application will not lcore_wait() for it.
159                  */
160                 if (lcore_config[lcore_id].core_role == ROLE_SERVICE)
161                         lcore_config[lcore_id].state = WAIT;
162                 else
163                         lcore_config[lcore_id].state = FINISHED;
164         }
165
166         /* never reached */
167         /* pthread_exit(NULL); */
168         /* return NULL; */
169 }
170
171 /* require calling thread tid by gettid() */
172 int rte_sys_gettid(void)
173 {
174         return (int)syscall(SYS_gettid);
175 }
176
177 int rte_thread_setname(pthread_t id, const char *name)
178 {
179         int ret = ENOSYS;
180 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
181 #if __GLIBC_PREREQ(2, 12)
182         ret = pthread_setname_np(id, name);
183 #endif
184 #endif
185         RTE_SET_USED(id);
186         RTE_SET_USED(name);
187         return -ret;
188 }