New upstream version 17.11.4
[deb_dpdk.git] / lib / librte_eal / linuxapp / eal / eal_thread.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <unistd.h>
39 #include <pthread.h>
40 #include <sched.h>
41 #include <sys/queue.h>
42 #include <sys/syscall.h>
43
44 #include <rte_debug.h>
45 #include <rte_atomic.h>
46 #include <rte_launch.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_per_lcore.h>
50 #include <rte_eal.h>
51 #include <rte_lcore.h>
52
53 #include "eal_private.h"
54 #include "eal_thread.h"
55
56 RTE_DEFINE_PER_LCORE(unsigned, _lcore_id) = LCORE_ID_ANY;
57 RTE_DEFINE_PER_LCORE(unsigned, _socket_id) = (unsigned)SOCKET_ID_ANY;
58 RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
59
60 /*
61  * Send a message to a slave lcore identified by slave_id to call a
62  * function f with argument arg. Once the execution is done, the
63  * remote lcore switch in FINISHED state.
64  */
65 int
66 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
67 {
68         int n;
69         char c = 0;
70         int m2s = lcore_config[slave_id].pipe_master2slave[1];
71         int s2m = lcore_config[slave_id].pipe_slave2master[0];
72
73         if (lcore_config[slave_id].state != WAIT)
74                 return -EBUSY;
75
76         lcore_config[slave_id].f = f;
77         lcore_config[slave_id].arg = arg;
78
79         /* send message */
80         n = 0;
81         while (n == 0 || (n < 0 && errno == EINTR))
82                 n = write(m2s, &c, 1);
83         if (n < 0)
84                 rte_panic("cannot write on configuration pipe\n");
85
86         /* wait ack */
87         do {
88                 n = read(s2m, &c, 1);
89         } while (n < 0 && errno == EINTR);
90
91         if (n <= 0)
92                 rte_panic("cannot read on configuration pipe\n");
93
94         return 0;
95 }
96
97 /* set affinity for current EAL thread */
98 static int
99 eal_thread_set_affinity(void)
100 {
101         unsigned lcore_id = rte_lcore_id();
102
103         /* acquire system unique id  */
104         rte_gettid();
105
106         /* update EAL thread core affinity */
107         return rte_thread_set_affinity(&lcore_config[lcore_id].cpuset);
108 }
109
110 void eal_thread_init_master(unsigned lcore_id)
111 {
112         /* set the lcore ID in per-lcore memory area */
113         RTE_PER_LCORE(_lcore_id) = lcore_id;
114
115         /* set CPU affinity */
116         if (eal_thread_set_affinity() < 0)
117                 rte_panic("cannot set affinity\n");
118 }
119
120 /* main loop of threads */
121 __attribute__((noreturn)) void *
122 eal_thread_loop(__attribute__((unused)) void *arg)
123 {
124         char c;
125         int n, ret;
126         unsigned lcore_id;
127         pthread_t thread_id;
128         int m2s, s2m;
129         char cpuset[RTE_CPU_AFFINITY_STR_LEN];
130
131         thread_id = pthread_self();
132
133         /* retrieve our lcore_id from the configuration structure */
134         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
135                 if (thread_id == lcore_config[lcore_id].thread_id)
136                         break;
137         }
138         if (lcore_id == RTE_MAX_LCORE)
139                 rte_panic("cannot retrieve lcore id\n");
140
141         m2s = lcore_config[lcore_id].pipe_master2slave[0];
142         s2m = lcore_config[lcore_id].pipe_slave2master[1];
143
144         /* set the lcore ID in per-lcore memory area */
145         RTE_PER_LCORE(_lcore_id) = lcore_id;
146
147         /* set CPU affinity */
148         if (eal_thread_set_affinity() < 0)
149                 rte_panic("cannot set affinity\n");
150
151         ret = eal_thread_dump_affinity(cpuset, RTE_CPU_AFFINITY_STR_LEN);
152
153         RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%x;cpuset=[%s%s])\n",
154                 lcore_id, (int)thread_id, cpuset, ret == 0 ? "" : "...");
155
156         /* read on our pipe to get commands */
157         while (1) {
158                 void *fct_arg;
159
160                 /* wait command */
161                 do {
162                         n = read(m2s, &c, 1);
163                 } while (n < 0 && errno == EINTR);
164
165                 if (n <= 0)
166                         rte_panic("cannot read on configuration pipe\n");
167
168                 lcore_config[lcore_id].state = RUNNING;
169
170                 /* send ack */
171                 n = 0;
172                 while (n == 0 || (n < 0 && errno == EINTR))
173                         n = write(s2m, &c, 1);
174                 if (n < 0)
175                         rte_panic("cannot write on configuration pipe\n");
176
177                 if (lcore_config[lcore_id].f == NULL)
178                         rte_panic("NULL function pointer\n");
179
180                 /* call the function and store the return value */
181                 fct_arg = lcore_config[lcore_id].arg;
182                 ret = lcore_config[lcore_id].f(fct_arg);
183                 lcore_config[lcore_id].ret = ret;
184                 rte_wmb();
185
186                 /* when a service core returns, it should go directly to WAIT
187                  * state, because the application will not lcore_wait() for it.
188                  */
189                 if (lcore_config[lcore_id].core_role == ROLE_SERVICE)
190                         lcore_config[lcore_id].state = WAIT;
191                 else
192                         lcore_config[lcore_id].state = FINISHED;
193         }
194
195         /* never reached */
196         /* pthread_exit(NULL); */
197         /* return NULL; */
198 }
199
200 /* require calling thread tid by gettid() */
201 int rte_sys_gettid(void)
202 {
203         return (int)syscall(SYS_gettid);
204 }
205
206 int rte_thread_setname(pthread_t id, const char *name)
207 {
208         int ret = ENOSYS;
209 #if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
210 #if __GLIBC_PREREQ(2, 12)
211         ret = pthread_setname_np(id, name);
212 #endif
213 #endif
214         RTE_SET_USED(id);
215         RTE_SET_USED(name);
216         return -ret;
217 }