first public release
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_thread.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stdint.h>
40 #include <unistd.h>
41 #include <pthread.h>
42 #include <sched.h>
43 #include <sys/queue.h>
44
45 #include <rte_debug.h>
46 #include <rte_atomic.h>
47 #include <rte_launch.h>
48 #include <rte_log.h>
49 #include <rte_memory.h>
50 #include <rte_memzone.h>
51 #include <rte_per_lcore.h>
52 #include <rte_tailq.h>
53 #include <rte_eal.h>
54 #include <rte_per_lcore.h>
55 #include <rte_lcore.h>
56
57 #include "eal_private.h"
58 #include "eal_thread.h"
59
60 RTE_DEFINE_PER_LCORE(unsigned, _lcore_id);
61
62 /*
63  * Send a message to a slave lcore identified by slave_id to call a
64  * function f with argument arg. Once the execution is done, the
65  * remote lcore switch in FINISHED state.
66  */
67 int
68 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
69 {
70         int n;
71         char c = 0;
72         int m2s = lcore_config[slave_id].pipe_master2slave[1];
73         int s2m = lcore_config[slave_id].pipe_slave2master[0];
74
75         if (lcore_config[slave_id].state != WAIT)
76                 return -EBUSY;
77
78         lcore_config[slave_id].f = f;
79         lcore_config[slave_id].arg = arg;
80
81         /* send message */
82         n = 0;
83         while (n == 0 || (n < 0 && errno == EINTR))
84                 n = write(m2s, &c, 1);
85         if (n < 0)
86                 rte_panic("cannot write on configuration pipe\n");
87
88         /* wait ack */
89         n = 0;
90         do {
91                 n = read(s2m, &c, 1);
92         } while (n < 0 && errno == EINTR);
93
94         if (n <= 0)
95                 rte_panic("cannot read on configuration pipe\n");
96
97         return 0;
98 }
99
100 /* set affinity for current thread */
101 static int
102 eal_thread_set_affinity(void)
103 {
104         int s;
105         pthread_t thread;
106
107 /*
108  * According to the section VERSIONS of the CPU_ALLOC man page:
109  *
110  * The CPU_ZERO(), CPU_SET(), CPU_CLR(), and CPU_ISSET() macros were added
111  * in glibc 2.3.3.
112  *
113  * CPU_COUNT() first appeared in glibc 2.6.
114  *
115  * CPU_AND(),     CPU_OR(),     CPU_XOR(),    CPU_EQUAL(),    CPU_ALLOC(),
116  * CPU_ALLOC_SIZE(), CPU_FREE(), CPU_ZERO_S(),  CPU_SET_S(),  CPU_CLR_S(),
117  * CPU_ISSET_S(),  CPU_AND_S(), CPU_OR_S(), CPU_XOR_S(), and CPU_EQUAL_S()
118  * first appeared in glibc 2.7.
119  */
120 #if defined(CPU_ALLOC)
121         size_t size;
122         cpu_set_t *cpusetp;
123
124         cpusetp = CPU_ALLOC(RTE_MAX_LCORE);
125         if (cpusetp == NULL) {
126                 RTE_LOG(ERR, EAL, "CPU_ALLOC failed\n");
127                 return -1;
128         }
129
130         size = CPU_ALLOC_SIZE(RTE_MAX_LCORE);
131         CPU_ZERO_S(size, cpusetp);
132         CPU_SET_S(rte_lcore_id(), size, cpusetp);
133
134         thread = pthread_self();
135         s = pthread_setaffinity_np(thread, size, cpusetp);
136         if (s != 0) {
137                 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
138                 CPU_FREE(cpusetp);
139                 return -1;
140         }
141
142         CPU_FREE(cpusetp);
143 #else /* CPU_ALLOC */
144         cpu_set_t cpuset;
145         CPU_ZERO( &cpuset );
146         CPU_SET( rte_lcore_id(), &cpuset );
147
148         thread = pthread_self();
149         s = pthread_setaffinity_np(thread, sizeof( cpuset ), &cpuset);
150         if (s != 0) {
151                 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
152                 return -1;
153         }
154 #endif
155         return 0;
156 }
157
158 void eal_thread_init_master(unsigned lcore_id)
159 {
160         /* set the lcore ID in per-lcore memory area */
161         RTE_PER_LCORE(_lcore_id) = lcore_id;
162
163         /* set CPU affinity */
164         if (eal_thread_set_affinity() < 0)
165                 rte_panic("cannot set affinity\n");
166 }
167
168 /* main loop of threads */
169 __attribute__((noreturn)) void *
170 eal_thread_loop(__attribute__((unused)) void *arg)
171 {
172         char c;
173         int n, ret;
174         unsigned lcore_id;
175         pthread_t thread_id;
176         int m2s, s2m;
177
178         thread_id = pthread_self();
179
180         /* retrieve our lcore_id from the configuration structure */
181         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
182                 if (thread_id == lcore_config[lcore_id].thread_id)
183                         break;
184         }
185         if (lcore_id == RTE_MAX_LCORE)
186                 rte_panic("cannot retrieve lcore id\n");
187
188         RTE_LOG(DEBUG, EAL, "Core %u is ready (tid=%x)\n",
189                 lcore_id, (int)thread_id);
190
191         m2s = lcore_config[lcore_id].pipe_master2slave[0];
192         s2m = lcore_config[lcore_id].pipe_slave2master[1];
193
194         /* set the lcore ID in per-lcore memory area */
195         RTE_PER_LCORE(_lcore_id) = lcore_id;
196
197         /* set CPU affinity */
198         if (eal_thread_set_affinity() < 0)
199                 rte_panic("cannot set affinity\n");
200
201         /* read on our pipe to get commands */
202         while (1) {
203                 void *fct_arg;
204
205                 /* wait command */
206                 n = 0;
207                 do {
208                         n = read(m2s, &c, 1);
209                 } while (n < 0 && errno == EINTR);
210
211                 if (n <= 0)
212                         rte_panic("cannot read on configuration pipe\n");
213
214                 lcore_config[lcore_id].state = RUNNING;
215
216                 /* send ack */
217                 n = 0;
218                 while (n == 0 || (n < 0 && errno == EINTR))
219                         n = write(s2m, &c, 1);
220                 if (n < 0)
221                         rte_panic("cannot write on configuration pipe\n");
222
223                 if (lcore_config[lcore_id].f == NULL)
224                         rte_panic("NULL function pointer\n");
225
226                 /* call the function and store the return value */
227                 fct_arg = lcore_config[lcore_id].arg;
228                 ret = lcore_config[lcore_id].f(fct_arg);
229                 lcore_config[lcore_id].ret = ret;
230                 rte_wmb();
231                 lcore_config[lcore_id].state = FINISHED;
232         }
233
234         /* never reached */
235         /* pthread_exit(NULL); */
236         /* return NULL; */
237 }