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