update Intel copyright years to 2014
[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
43 #include <rte_debug.h>
44 #include <rte_atomic.h>
45 #include <rte_launch.h>
46 #include <rte_log.h>
47 #include <rte_memory.h>
48 #include <rte_memzone.h>
49 #include <rte_per_lcore.h>
50 #include <rte_tailq.h>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54
55 #include "eal_private.h"
56 #include "eal_thread.h"
57
58 RTE_DEFINE_PER_LCORE(unsigned, _lcore_id);
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 thread */
98 static int
99 eal_thread_set_affinity(void)
100 {
101         int s;
102         pthread_t thread;
103
104 /*
105  * According to the section VERSIONS of the CPU_ALLOC man page:
106  *
107  * The CPU_ZERO(), CPU_SET(), CPU_CLR(), and CPU_ISSET() macros were added
108  * in glibc 2.3.3.
109  *
110  * CPU_COUNT() first appeared in glibc 2.6.
111  *
112  * CPU_AND(),     CPU_OR(),     CPU_XOR(),    CPU_EQUAL(),    CPU_ALLOC(),
113  * CPU_ALLOC_SIZE(), CPU_FREE(), CPU_ZERO_S(),  CPU_SET_S(),  CPU_CLR_S(),
114  * CPU_ISSET_S(),  CPU_AND_S(), CPU_OR_S(), CPU_XOR_S(), and CPU_EQUAL_S()
115  * first appeared in glibc 2.7.
116  */
117 #if defined(CPU_ALLOC)
118         size_t size;
119         cpu_set_t *cpusetp;
120
121         cpusetp = CPU_ALLOC(RTE_MAX_LCORE);
122         if (cpusetp == NULL) {
123                 RTE_LOG(ERR, EAL, "CPU_ALLOC failed\n");
124                 return -1;
125         }
126
127         size = CPU_ALLOC_SIZE(RTE_MAX_LCORE);
128         CPU_ZERO_S(size, cpusetp);
129         CPU_SET_S(rte_lcore_id(), size, cpusetp);
130
131         thread = pthread_self();
132         s = pthread_setaffinity_np(thread, size, cpusetp);
133         if (s != 0) {
134                 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
135                 CPU_FREE(cpusetp);
136                 return -1;
137         }
138
139         CPU_FREE(cpusetp);
140 #else /* CPU_ALLOC */
141         cpu_set_t cpuset;
142         CPU_ZERO( &cpuset );
143         CPU_SET( rte_lcore_id(), &cpuset );
144
145         thread = pthread_self();
146         s = pthread_setaffinity_np(thread, sizeof( cpuset ), &cpuset);
147         if (s != 0) {
148                 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
149                 return -1;
150         }
151 #endif
152         return 0;
153 }
154
155 void eal_thread_init_master(unsigned lcore_id)
156 {
157         /* set the lcore ID in per-lcore memory area */
158         RTE_PER_LCORE(_lcore_id) = lcore_id;
159
160         /* set CPU affinity */
161         if (eal_thread_set_affinity() < 0)
162                 rte_panic("cannot set affinity\n");
163 }
164
165 /* main loop of threads */
166 __attribute__((noreturn)) void *
167 eal_thread_loop(__attribute__((unused)) void *arg)
168 {
169         char c;
170         int n, ret;
171         unsigned lcore_id;
172         pthread_t thread_id;
173         int m2s, s2m;
174
175         thread_id = pthread_self();
176
177         /* retrieve our lcore_id from the configuration structure */
178         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
179                 if (thread_id == lcore_config[lcore_id].thread_id)
180                         break;
181         }
182         if (lcore_id == RTE_MAX_LCORE)
183                 rte_panic("cannot retrieve lcore id\n");
184
185         RTE_LOG(DEBUG, EAL, "Core %u is ready (tid=%x)\n",
186                 lcore_id, (int)thread_id);
187
188         m2s = lcore_config[lcore_id].pipe_master2slave[0];
189         s2m = lcore_config[lcore_id].pipe_slave2master[1];
190
191         /* set the lcore ID in per-lcore memory area */
192         RTE_PER_LCORE(_lcore_id) = lcore_id;
193
194         /* set CPU affinity */
195         if (eal_thread_set_affinity() < 0)
196                 rte_panic("cannot set affinity\n");
197
198         /* read on our pipe to get commands */
199         while (1) {
200                 void *fct_arg;
201
202                 /* wait command */
203                 do {
204                         n = read(m2s, &c, 1);
205                 } while (n < 0 && errno == EINTR);
206
207                 if (n <= 0)
208                         rte_panic("cannot read on configuration pipe\n");
209
210                 lcore_config[lcore_id].state = RUNNING;
211
212                 /* send ack */
213                 n = 0;
214                 while (n == 0 || (n < 0 && errno == EINTR))
215                         n = write(s2m, &c, 1);
216                 if (n < 0)
217                         rte_panic("cannot write on configuration pipe\n");
218
219                 if (lcore_config[lcore_id].f == NULL)
220                         rte_panic("NULL function pointer\n");
221
222                 /* call the function and store the return value */
223                 fct_arg = lcore_config[lcore_id].arg;
224                 ret = lcore_config[lcore_id].f(fct_arg);
225                 lcore_config[lcore_id].ret = ret;
226                 rte_wmb();
227                 lcore_config[lcore_id].state = FINISHED;
228         }
229
230         /* never reached */
231         /* pthread_exit(NULL); */
232         /* return NULL; */
233 }