New upstream version 18.02
[deb_dpdk.git] / examples / multi_process / l2fwd_fork / flib.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include <inttypes.h>
10 #include <sys/types.h>
11 #include <sys/queue.h>
12 #include <sys/wait.h>
13 #include <sys/prctl.h>
14 #include <netinet/in.h>
15 #include <setjmp.h>
16 #include <stdarg.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <getopt.h>
20 #include <dirent.h>
21 #include <signal.h>
22
23 #include <rte_common.h>
24 #include <rte_log.h>
25 #include <rte_malloc.h>
26 #include <rte_memory.h>
27 #include <rte_memcpy.h>
28 #include <rte_eal.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>
42 #include <rte_mbuf.h>
43 #include <rte_string_fns.h>
44
45 #include "flib.h"
46
47 #define SIG_PARENT_EXIT SIGUSR1
48
49 struct lcore_stat {
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;
55
56
57 static struct lcore_stat *core_cfg;
58 static uint16_t *lcore_cfg = NULL;
59
60 /* signal handler to be notified after parent leaves */
61 static void
62 sighand_parent_exit(int sig)
63 {
64         printf("lcore = %u : Find parent leaves, sig=%d\n", rte_lcore_id(),
65                         sig);
66         printf("Child leaving\n");
67         exit(0);
68
69         return;
70 }
71
72 /**
73  * Real function entrance ran in slave process
74  **/
75 static int
76 slave_proc_func(void)
77 {
78         struct rte_config *config;
79         unsigned slave_id = rte_lcore_id();
80         struct lcore_stat *cfg = &core_cfg[slave_id];
81
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");
85         else {
86                 struct sigaction act;
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);
91         }
92
93         /* Set slave process to SECONDARY to avoid operation like dev_start/stop etc */
94         config = rte_eal_get_configuration();
95         if (NULL == config)
96                 printf("Warning:Can't get rte_config\n");
97         else
98                 config->process_type = RTE_PROC_SECONDARY;
99
100         printf("Core %u is ready (pid=%d)\n", slave_id, (int)cfg->pid);
101
102         exit(cfg->f(cfg->arg));
103 }
104
105 /**
106  * function entrance ran in master thread, which will spawn slave process and wait until
107  * specific slave exited.
108  **/
109 static int
110 lcore_func(void *arg __attribute__((unused)))
111 {
112         unsigned slave_id = rte_lcore_id();
113         struct lcore_stat *cfg = &core_cfg[slave_id];
114         int pid, stat;
115
116         if (rte_get_master_lcore() == slave_id)
117                 return cfg->f(cfg->arg);
118
119         /* fork a slave process */
120         pid = fork();
121
122         if (pid == -1) {
123                 printf("Failed to fork\n");
124                 return -1;
125         } else if (pid == 0) /* child */
126                 return slave_proc_func();
127         else { /* parent */
128                 cfg->pid = pid;
129
130                 waitpid(pid, &stat, 0);
131
132                 cfg->pid = 0;
133                 cfg->f = NULL;
134                 cfg->arg = NULL;
135                 /* Notify slave's exit if applicable */
136                 if(cfg->cb_fn)
137                         cfg->cb_fn(slave_id, stat);
138                 return stat;
139         }
140 }
141
142 static int
143 lcore_id_init(void)
144 {
145         int i;
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);
150
151         if(lcore_cfg == NULL)
152                 rte_panic("Failed to malloc\n");
153
154         for (i = 0; i < RTE_MAX_LCORE; i++) {
155                 if (rte_lcore_is_enabled(i))
156                         lcore_cfg[i] = 1;
157         }
158         return 0;
159 }
160
161 int
162 flib_assign_lcore_id(void)
163 {
164         unsigned i;
165         int ret;
166
167         /**
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.
171          **/
172         if (rte_lcore_id() != 0)
173                 return -1;
174
175         do {
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)
179                                 break;
180                 }
181                 if (i == RTE_MAX_LCORE)
182                         return -1;
183
184                 /* Assign new lcore id to this thread */
185
186                 ret = rte_atomic16_cmpset(&lcore_cfg[i], 0, 1);
187         } while (unlikely(ret == 0));
188
189         RTE_PER_LCORE(_lcore_id) = i;
190         return i;
191 }
192
193 void
194 flib_free_lcore_id(unsigned lcore_id)
195 {
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))
199                 return;
200
201         lcore_cfg[lcore_id] = 0;
202 }
203
204 int
205 flib_register_slave_exit_notify(unsigned slave_id,
206         slave_exit_notify *cb)
207 {
208         if (cb == NULL)
209                 return -EFAULT;
210
211         if (!rte_lcore_is_enabled(slave_id))
212                 return -ENOENT;
213
214         core_cfg[slave_id].cb_fn = cb;
215
216         return 0;
217 }
218
219 enum slave_stat
220 flib_query_slave_status(unsigned slave_id)
221 {
222         if (!rte_lcore_is_enabled(slave_id))
223                 return ST_FREEZE;
224         /* pid only be set when slave process spawned */
225         if (core_cfg[slave_id].pid != 0)
226                 return ST_RUN;
227         else
228                 return ST_IDLE;
229 }
230
231 int
232 flib_remote_launch(lcore_function_t *f,
233                                         void *arg, unsigned slave_id)
234 {
235         if (f == NULL)
236                 return -1;
237
238         if (!rte_lcore_is_enabled(slave_id))
239                 return -1;
240
241         /* Wait until specific lcore state change to WAIT */
242         rte_eal_wait_lcore(slave_id);
243
244         core_cfg[slave_id].f = f;
245         core_cfg[slave_id].arg = arg;
246
247         return rte_eal_remote_launch(lcore_func, NULL, slave_id);
248 }
249
250 int
251 flib_mp_remote_launch(lcore_function_t *f, void *arg,
252                         enum rte_rmt_call_master_t call_master)
253 {
254         int i;
255
256         RTE_LCORE_FOREACH_SLAVE(i) {
257                 core_cfg[i].arg = arg;
258                 core_cfg[i].f = f;
259         }
260
261         return rte_eal_mp_remote_launch(lcore_func, NULL, call_master);
262 }
263
264 int
265 flib_init(void)
266 {
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");
271                 return -1;
272         }
273
274         if (lcore_id_init() != 0) {
275                 printf("lcore_id_init failed\n");
276                 return -1;
277         }
278
279         return 0;
280 }