4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
42 #include <sys/queue.h>
44 #include <rte_debug.h>
45 #include <rte_atomic.h>
46 #include <rte_launch.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_per_lcore.h>
51 #include <rte_tailq.h>
53 #include <rte_per_lcore.h>
54 #include <rte_lcore.h>
56 #include "eal_private.h"
57 #include "eal_thread.h"
59 RTE_DEFINE_PER_LCORE(unsigned, _lcore_id);
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.
67 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
71 int m2s = lcore_config[slave_id].pipe_master2slave[1];
72 int s2m = lcore_config[slave_id].pipe_slave2master[0];
74 if (lcore_config[slave_id].state != WAIT)
77 lcore_config[slave_id].f = f;
78 lcore_config[slave_id].arg = arg;
82 while (n == 0 || (n < 0 && errno == EINTR))
83 n = write(m2s, &c, 1);
85 rte_panic("cannot write on configuration pipe\n");
91 } while (n < 0 && errno == EINTR);
94 rte_panic("cannot read on configuration pipe\n");
99 /* set affinity for current thread */
101 eal_thread_set_affinity(void)
107 * According to the section VERSIONS of the CPU_ALLOC man page:
109 * The CPU_ZERO(), CPU_SET(), CPU_CLR(), and CPU_ISSET() macros were added
112 * CPU_COUNT() first appeared in glibc 2.6.
114 * CPU_AND(), CPU_OR(), CPU_XOR(), CPU_EQUAL(), CPU_ALLOC(),
115 * CPU_ALLOC_SIZE(), CPU_FREE(), CPU_ZERO_S(), CPU_SET_S(), CPU_CLR_S(),
116 * CPU_ISSET_S(), CPU_AND_S(), CPU_OR_S(), CPU_XOR_S(), and CPU_EQUAL_S()
117 * first appeared in glibc 2.7.
119 #if defined(CPU_ALLOC)
123 cpusetp = CPU_ALLOC(RTE_MAX_LCORE);
124 if (cpusetp == NULL) {
125 RTE_LOG(ERR, EAL, "CPU_ALLOC failed\n");
129 size = CPU_ALLOC_SIZE(RTE_MAX_LCORE);
130 CPU_ZERO_S(size, cpusetp);
131 CPU_SET_S(rte_lcore_id(), size, cpusetp);
133 thread = pthread_self();
134 s = pthread_setaffinity_np(thread, size, cpusetp);
136 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
142 #else /* CPU_ALLOC */
145 CPU_SET( rte_lcore_id(), &cpuset );
147 thread = pthread_self();
148 s = pthread_setaffinity_np(thread, sizeof( cpuset ), &cpuset);
150 RTE_LOG(ERR, EAL, "pthread_setaffinity_np failed\n");
157 void eal_thread_init_master(unsigned lcore_id)
159 /* set the lcore ID in per-lcore memory area */
160 RTE_PER_LCORE(_lcore_id) = lcore_id;
162 /* set CPU affinity */
163 if (eal_thread_set_affinity() < 0)
164 rte_panic("cannot set affinity\n");
167 /* main loop of threads */
168 __attribute__((noreturn)) void *
169 eal_thread_loop(__attribute__((unused)) void *arg)
177 thread_id = pthread_self();
179 /* retrieve our lcore_id from the configuration structure */
180 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
181 if (thread_id == lcore_config[lcore_id].thread_id)
184 if (lcore_id == RTE_MAX_LCORE)
185 rte_panic("cannot retrieve lcore id\n");
187 RTE_LOG(DEBUG, EAL, "Core %u is ready (tid=%x)\n",
188 lcore_id, (int)thread_id);
190 m2s = lcore_config[lcore_id].pipe_master2slave[0];
191 s2m = lcore_config[lcore_id].pipe_slave2master[1];
193 /* set the lcore ID in per-lcore memory area */
194 RTE_PER_LCORE(_lcore_id) = lcore_id;
196 /* set CPU affinity */
197 if (eal_thread_set_affinity() < 0)
198 rte_panic("cannot set affinity\n");
200 /* read on our pipe to get commands */
207 n = read(m2s, &c, 1);
208 } while (n < 0 && errno == EINTR);
211 rte_panic("cannot read on configuration pipe\n");
213 lcore_config[lcore_id].state = RUNNING;
217 while (n == 0 || (n < 0 && errno == EINTR))
218 n = write(s2m, &c, 1);
220 rte_panic("cannot write on configuration pipe\n");
222 if (lcore_config[lcore_id].f == NULL)
223 rte_panic("NULL function pointer\n");
225 /* call the function and store the return value */
226 fct_arg = lcore_config[lcore_id].arg;
227 ret = lcore_config[lcore_id].f(fct_arg);
228 lcore_config[lcore_id].ret = ret;
230 lcore_config[lcore_id].state = FINISHED;
234 /* pthread_exit(NULL); */