4 * Copyright(c) 2010-2014 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.
39 #include <sys/types.h>
40 #include <sys/queue.h>
42 #include <sys/prctl.h>
43 #include <netinet/in.h>
52 #include <rte_common.h>
54 #include <rte_malloc.h>
55 #include <rte_memory.h>
56 #include <rte_memcpy.h>
57 #include <rte_memzone.h>
59 #include <rte_launch.h>
60 #include <rte_atomic.h>
61 #include <rte_cycles.h>
62 #include <rte_prefetch.h>
63 #include <rte_lcore.h>
64 #include <rte_per_lcore.h>
65 #include <rte_branch_prediction.h>
66 #include <rte_interrupts.h>
68 #include <rte_random.h>
69 #include <rte_debug.h>
70 #include <rte_ether.h>
71 #include <rte_ethdev.h>
72 #include <rte_mempool.h>
74 #include <rte_string_fns.h>
78 #define SIG_PARENT_EXIT SIGUSR1
81 pid_t pid; /**< pthread identifier */
82 lcore_function_t *f; /**< function to call */
83 void *arg; /**< argument of function */
84 slave_exit_notify *cb_fn;
85 } __rte_cache_aligned;
88 static struct lcore_stat *core_cfg;
89 static uint16_t *lcore_cfg = NULL;
91 /* signal handler to be notified after parent leaves */
93 sighand_parent_exit(int sig)
95 printf("lcore = %u : Find parent leaves, sig=%d\n", rte_lcore_id(),
97 printf("Child leaving\n");
104 * Real function entrance ran in slave process
107 slave_proc_func(void)
109 struct rte_config *config;
110 unsigned slave_id = rte_lcore_id();
111 struct lcore_stat *cfg = &core_cfg[slave_id];
113 if (prctl(PR_SET_PDEATHSIG, SIG_PARENT_EXIT, 0, 0, 0, 0) != 0)
114 printf("Warning: Slave can't register for being notified in"
115 "case master process exited\n");
117 struct sigaction act;
118 memset(&act, 0 , sizeof(act));
119 act.sa_handler = sighand_parent_exit;
120 if (sigaction(SIG_PARENT_EXIT, &act, NULL) != 0)
121 printf("Fail to register signal handler:%d\n", SIG_PARENT_EXIT);
124 /* Set slave process to SECONDARY to avoid operation like dev_start/stop etc */
125 config = rte_eal_get_configuration();
127 printf("Warning:Can't get rte_config\n");
129 config->process_type = RTE_PROC_SECONDARY;
131 printf("Core %u is ready (pid=%d)\n", slave_id, (int)cfg->pid);
133 exit(cfg->f(cfg->arg));
137 * function entrance ran in master thread, which will spawn slave process and wait until
138 * specific slave exited.
141 lcore_func(void *arg __attribute__((unused)))
143 unsigned slave_id = rte_lcore_id();
144 struct lcore_stat *cfg = &core_cfg[slave_id];
147 if (rte_get_master_lcore() == slave_id)
148 return cfg->f(cfg->arg);
150 /* fork a slave process */
154 printf("Failed to fork\n");
156 } else if (pid == 0) /* child */
157 return slave_proc_func();
161 waitpid(pid, &stat, 0);
166 /* Notify slave's exit if applicable */
168 cfg->cb_fn(slave_id, stat);
177 /* Setup lcore ID allocation map */
178 lcore_cfg = rte_zmalloc("LCORE_ID_MAP",
179 sizeof(uint16_t) * RTE_MAX_LCORE,
180 RTE_CACHE_LINE_SIZE);
182 if(lcore_cfg == NULL)
183 rte_panic("Failed to malloc\n");
185 for (i = 0; i < RTE_MAX_LCORE; i++) {
186 if (rte_lcore_is_enabled(i))
193 flib_assign_lcore_id(void)
199 * thread assigned a lcore id previously, or a slave thread. But still have
200 * a bug here: If the core mask includes core 0, and that core call this
201 * function, it still can get a new lcore id.
203 if (rte_lcore_id() != 0)
207 /* Find a lcore id not used yet, avoid to use lcore ID 0 */
208 for (i = 1; i < RTE_MAX_LCORE; i++) {
209 if (lcore_cfg[i] == 0)
212 if (i == RTE_MAX_LCORE)
215 /* Assign new lcore id to this thread */
217 ret = rte_atomic16_cmpset(&lcore_cfg[i], 0, 1);
218 } while (unlikely(ret == 0));
220 RTE_PER_LCORE(_lcore_id) = i;
225 flib_free_lcore_id(unsigned lcore_id)
227 /* id is not valid or belongs to pinned core id */
228 if (lcore_id >= RTE_MAX_LCORE || lcore_id == 0 ||
229 rte_lcore_is_enabled(lcore_id))
232 lcore_cfg[lcore_id] = 0;
236 flib_register_slave_exit_notify(unsigned slave_id,
237 slave_exit_notify *cb)
242 if (!rte_lcore_is_enabled(slave_id))
245 core_cfg[slave_id].cb_fn = cb;
251 flib_query_slave_status(unsigned slave_id)
253 if (!rte_lcore_is_enabled(slave_id))
255 /* pid only be set when slave process spawned */
256 if (core_cfg[slave_id].pid != 0)
263 flib_remote_launch(lcore_function_t *f,
264 void *arg, unsigned slave_id)
269 if (!rte_lcore_is_enabled(slave_id))
272 /* Wait until specific lcore state change to WAIT */
273 rte_eal_wait_lcore(slave_id);
275 core_cfg[slave_id].f = f;
276 core_cfg[slave_id].arg = arg;
278 return rte_eal_remote_launch(lcore_func, NULL, slave_id);
282 flib_mp_remote_launch(lcore_function_t *f, void *arg,
283 enum rte_rmt_call_master_t call_master)
287 RTE_LCORE_FOREACH_SLAVE(i) {
288 core_cfg[i].arg = arg;
292 return rte_eal_mp_remote_launch(lcore_func, NULL, call_master);
298 if ((core_cfg = rte_zmalloc("core_cfg",
299 sizeof(struct lcore_stat) * RTE_MAX_LCORE,
300 RTE_CACHE_LINE_SIZE)) == NULL ) {
301 printf("rte_zmalloc failed\n");
305 if (lcore_id_init() != 0) {
306 printf("lcore_id_init failed\n");