1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <sys/types.h>
11 #include <sys/queue.h>
13 #include <sys/prctl.h>
14 #include <netinet/in.h>
23 #include <rte_common.h>
25 #include <rte_malloc.h>
26 #include <rte_memory.h>
27 #include <rte_memcpy.h>
29 #include <rte_launch.h>
30 #include <rte_atomic.h>
31 #include <rte_cycles.h>
32 #include <rte_prefetch.h>
33 #include <rte_lcore.h>
34 #include <rte_per_lcore.h>
35 #include <rte_branch_prediction.h>
36 #include <rte_interrupts.h>
37 #include <rte_random.h>
38 #include <rte_debug.h>
39 #include <rte_ether.h>
40 #include <rte_ethdev.h>
41 #include <rte_mempool.h>
43 #include <rte_string_fns.h>
47 #define SIG_PARENT_EXIT SIGUSR1
50 pid_t pid; /**< pthread identifier */
51 lcore_function_t *f; /**< function to call */
52 void *arg; /**< argument of function */
53 slave_exit_notify *cb_fn;
54 } __rte_cache_aligned;
57 static struct lcore_stat *core_cfg;
58 static uint16_t *lcore_cfg = NULL;
60 /* signal handler to be notified after parent leaves */
62 sighand_parent_exit(int sig)
64 printf("lcore = %u : Find parent leaves, sig=%d\n", rte_lcore_id(),
66 printf("Child leaving\n");
73 * Real function entrance ran in slave process
78 struct rte_config *config;
79 unsigned slave_id = rte_lcore_id();
80 struct lcore_stat *cfg = &core_cfg[slave_id];
82 if (prctl(PR_SET_PDEATHSIG, SIG_PARENT_EXIT, 0, 0, 0, 0) != 0)
83 printf("Warning: Slave can't register for being notified in"
84 "case master process exited\n");
87 memset(&act, 0 , sizeof(act));
88 act.sa_handler = sighand_parent_exit;
89 if (sigaction(SIG_PARENT_EXIT, &act, NULL) != 0)
90 printf("Fail to register signal handler:%d\n", SIG_PARENT_EXIT);
93 /* Set slave process to SECONDARY to avoid operation like dev_start/stop etc */
94 config = rte_eal_get_configuration();
96 printf("Warning:Can't get rte_config\n");
98 config->process_type = RTE_PROC_SECONDARY;
100 printf("Core %u is ready (pid=%d)\n", slave_id, (int)cfg->pid);
102 exit(cfg->f(cfg->arg));
106 * function entrance ran in master thread, which will spawn slave process and wait until
107 * specific slave exited.
110 lcore_func(void *arg __attribute__((unused)))
112 unsigned slave_id = rte_lcore_id();
113 struct lcore_stat *cfg = &core_cfg[slave_id];
116 if (rte_get_master_lcore() == slave_id)
117 return cfg->f(cfg->arg);
119 /* fork a slave process */
123 printf("Failed to fork\n");
125 } else if (pid == 0) /* child */
126 return slave_proc_func();
130 waitpid(pid, &stat, 0);
135 /* Notify slave's exit if applicable */
137 cfg->cb_fn(slave_id, stat);
146 /* Setup lcore ID allocation map */
147 lcore_cfg = rte_zmalloc("LCORE_ID_MAP",
148 sizeof(uint16_t) * RTE_MAX_LCORE,
149 RTE_CACHE_LINE_SIZE);
151 if(lcore_cfg == NULL)
152 rte_panic("Failed to malloc\n");
154 for (i = 0; i < RTE_MAX_LCORE; i++) {
155 if (rte_lcore_is_enabled(i))
162 flib_assign_lcore_id(void)
168 * thread assigned a lcore id previously, or a slave thread. But still have
169 * a bug here: If the core mask includes core 0, and that core call this
170 * function, it still can get a new lcore id.
172 if (rte_lcore_id() != 0)
176 /* Find a lcore id not used yet, avoid to use lcore ID 0 */
177 for (i = 1; i < RTE_MAX_LCORE; i++) {
178 if (lcore_cfg[i] == 0)
181 if (i == RTE_MAX_LCORE)
184 /* Assign new lcore id to this thread */
186 ret = rte_atomic16_cmpset(&lcore_cfg[i], 0, 1);
187 } while (unlikely(ret == 0));
189 RTE_PER_LCORE(_lcore_id) = i;
194 flib_free_lcore_id(unsigned lcore_id)
196 /* id is not valid or belongs to pinned core id */
197 if (lcore_id >= RTE_MAX_LCORE || lcore_id == 0 ||
198 rte_lcore_is_enabled(lcore_id))
201 lcore_cfg[lcore_id] = 0;
205 flib_register_slave_exit_notify(unsigned slave_id,
206 slave_exit_notify *cb)
211 if (!rte_lcore_is_enabled(slave_id))
214 core_cfg[slave_id].cb_fn = cb;
220 flib_query_slave_status(unsigned slave_id)
222 if (!rte_lcore_is_enabled(slave_id))
224 /* pid only be set when slave process spawned */
225 if (core_cfg[slave_id].pid != 0)
232 flib_remote_launch(lcore_function_t *f,
233 void *arg, unsigned slave_id)
238 if (!rte_lcore_is_enabled(slave_id))
241 /* Wait until specific lcore state change to WAIT */
242 rte_eal_wait_lcore(slave_id);
244 core_cfg[slave_id].f = f;
245 core_cfg[slave_id].arg = arg;
247 return rte_eal_remote_launch(lcore_func, NULL, slave_id);
251 flib_mp_remote_launch(lcore_function_t *f, void *arg,
252 enum rte_rmt_call_master_t call_master)
256 RTE_LCORE_FOREACH_SLAVE(i) {
257 core_cfg[i].arg = arg;
261 return rte_eal_mp_remote_launch(lcore_func, NULL, call_master);
267 if ((core_cfg = rte_zmalloc("core_cfg",
268 sizeof(struct lcore_stat) * RTE_MAX_LCORE,
269 RTE_CACHE_LINE_SIZE)) == NULL ) {
270 printf("rte_zmalloc failed\n");
274 if (lcore_id_init() != 0) {
275 printf("lcore_id_init failed\n");