remove useless memzone includes
[dpdk.git] / examples / multi_process / l2fwd_fork / flib.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <unistd.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <sys/types.h>
40 #include <sys/queue.h>
41 #include <sys/wait.h>
42 #include <sys/prctl.h>
43 #include <netinet/in.h>
44 #include <setjmp.h>
45 #include <stdarg.h>
46 #include <ctype.h>
47 #include <errno.h>
48 #include <getopt.h>
49 #include <dirent.h>
50 #include <signal.h>
51
52 #include <rte_common.h>
53 #include <rte_log.h>
54 #include <rte_malloc.h>
55 #include <rte_memory.h>
56 #include <rte_memcpy.h>
57 #include <rte_eal.h>
58 #include <rte_launch.h>
59 #include <rte_atomic.h>
60 #include <rte_cycles.h>
61 #include <rte_prefetch.h>
62 #include <rte_lcore.h>
63 #include <rte_per_lcore.h>
64 #include <rte_branch_prediction.h>
65 #include <rte_interrupts.h>
66 #include <rte_pci.h>
67 #include <rte_random.h>
68 #include <rte_debug.h>
69 #include <rte_ether.h>
70 #include <rte_ethdev.h>
71 #include <rte_mempool.h>
72 #include <rte_mbuf.h>
73 #include <rte_string_fns.h>
74
75 #include "flib.h"
76
77 #define SIG_PARENT_EXIT SIGUSR1
78
79 struct lcore_stat {
80         pid_t pid;           /**< pthread identifier */
81         lcore_function_t *f; /**< function to call */
82         void *arg;           /**< argument of function */
83         slave_exit_notify *cb_fn;
84 } __rte_cache_aligned;
85
86
87 static struct lcore_stat *core_cfg;
88 static uint16_t *lcore_cfg = NULL;
89
90 /* signal handler to be notified after parent leaves */
91 static void
92 sighand_parent_exit(int sig)
93 {
94         printf("lcore = %u : Find parent leaves, sig=%d\n", rte_lcore_id(),
95                         sig);
96         printf("Child leaving\n");
97         exit(0);
98
99         return;
100 }
101
102 /**
103  * Real function entrance ran in slave process
104  **/
105 static int
106 slave_proc_func(void)
107 {
108         struct rte_config *config;
109         unsigned slave_id = rte_lcore_id();
110         struct lcore_stat *cfg = &core_cfg[slave_id];
111
112         if (prctl(PR_SET_PDEATHSIG, SIG_PARENT_EXIT, 0, 0, 0, 0) != 0)
113                 printf("Warning: Slave can't register for being notified in"
114                "case master process exited\n");
115         else {
116                 struct sigaction act;
117                 memset(&act, 0 , sizeof(act));
118                 act.sa_handler = sighand_parent_exit;
119                 if (sigaction(SIG_PARENT_EXIT, &act, NULL) != 0)
120                         printf("Fail to register signal handler:%d\n", SIG_PARENT_EXIT);
121         }
122
123         /* Set slave process to SECONDARY to avoid operation like dev_start/stop etc */
124         config = rte_eal_get_configuration();
125         if (NULL == config)
126                 printf("Warning:Can't get rte_config\n");
127         else
128                 config->process_type = RTE_PROC_SECONDARY;
129
130         printf("Core %u is ready (pid=%d)\n", slave_id, (int)cfg->pid);
131
132         exit(cfg->f(cfg->arg));
133 }
134
135 /**
136  * function entrance ran in master thread, which will spawn slave process and wait until
137  * specific slave exited.
138  **/
139 static int
140 lcore_func(void *arg __attribute__((unused)))
141 {
142         unsigned slave_id = rte_lcore_id();
143         struct lcore_stat *cfg = &core_cfg[slave_id];
144         int pid, stat;
145
146         if (rte_get_master_lcore() == slave_id)
147                 return cfg->f(cfg->arg);
148
149         /* fork a slave process */
150         pid = fork();
151
152         if (pid == -1) {
153                 printf("Failed to fork\n");
154                 return -1;
155         } else if (pid == 0) /* child */
156                 return slave_proc_func();
157         else { /* parent */
158                 cfg->pid = pid;
159
160                 waitpid(pid, &stat, 0);
161
162                 cfg->pid = 0;
163                 cfg->f = NULL;
164                 cfg->arg = NULL;
165                 /* Notify slave's exit if applicable */
166                 if(cfg->cb_fn)
167                         cfg->cb_fn(slave_id, stat);
168                 return stat;
169         }
170 }
171
172 static int
173 lcore_id_init(void)
174 {
175         int i;
176         /* Setup lcore ID allocation map */
177         lcore_cfg = rte_zmalloc("LCORE_ID_MAP",
178                                                 sizeof(uint16_t) * RTE_MAX_LCORE,
179                                                 RTE_CACHE_LINE_SIZE);
180
181         if(lcore_cfg == NULL)
182                 rte_panic("Failed to malloc\n");
183
184         for (i = 0; i < RTE_MAX_LCORE; i++) {
185                 if (rte_lcore_is_enabled(i))
186                         lcore_cfg[i] = 1;
187         }
188         return 0;
189 }
190
191 int
192 flib_assign_lcore_id(void)
193 {
194         unsigned i;
195         int ret;
196
197         /**
198          * thread assigned a lcore id previously, or a  slave thread. But still have
199          * a bug here: If the core mask includes core 0, and that core call this
200          * function, it still can get a new lcore id.
201          **/
202         if (rte_lcore_id() != 0)
203                 return -1;
204
205         do {
206                 /* Find a lcore id not used yet, avoid to use lcore ID 0 */
207                 for (i = 1; i < RTE_MAX_LCORE; i++) {
208                         if (lcore_cfg[i] == 0)
209                                 break;
210                 }
211                 if (i == RTE_MAX_LCORE)
212                         return -1;
213
214                 /* Assign new lcore id to this thread */
215
216                 ret = rte_atomic16_cmpset(&lcore_cfg[i], 0, 1);
217         } while (unlikely(ret == 0));
218
219         RTE_PER_LCORE(_lcore_id) = i;
220         return i;
221 }
222
223 void
224 flib_free_lcore_id(unsigned lcore_id)
225 {
226         /* id is not valid or belongs to pinned core id */
227         if (lcore_id >= RTE_MAX_LCORE || lcore_id == 0 ||
228                 rte_lcore_is_enabled(lcore_id))
229                 return;
230
231         lcore_cfg[lcore_id] = 0;
232 }
233
234 int
235 flib_register_slave_exit_notify(unsigned slave_id,
236         slave_exit_notify *cb)
237 {
238         if (cb == NULL)
239                 return -EFAULT;
240
241         if (!rte_lcore_is_enabled(slave_id))
242                 return -ENOENT;
243
244         core_cfg[slave_id].cb_fn = cb;
245
246         return 0;
247 }
248
249 enum slave_stat
250 flib_query_slave_status(unsigned slave_id)
251 {
252         if (!rte_lcore_is_enabled(slave_id))
253                 return ST_FREEZE;
254         /* pid only be set when slave process spawned */
255         if (core_cfg[slave_id].pid != 0)
256                 return ST_RUN;
257         else
258                 return ST_IDLE;
259 }
260
261 int
262 flib_remote_launch(lcore_function_t *f,
263                                         void *arg, unsigned slave_id)
264 {
265         if (f == NULL)
266                 return -1;
267
268         if (!rte_lcore_is_enabled(slave_id))
269                 return -1;
270
271         /* Wait until specific lcore state change to WAIT */
272         rte_eal_wait_lcore(slave_id);
273
274         core_cfg[slave_id].f = f;
275         core_cfg[slave_id].arg = arg;
276
277         return rte_eal_remote_launch(lcore_func, NULL, slave_id);
278 }
279
280 int
281 flib_mp_remote_launch(lcore_function_t *f, void *arg,
282                         enum rte_rmt_call_master_t call_master)
283 {
284         int i;
285
286         RTE_LCORE_FOREACH_SLAVE(i) {
287                 core_cfg[i].arg = arg;
288                 core_cfg[i].f = f;
289         }
290
291         return rte_eal_mp_remote_launch(lcore_func, NULL, call_master);
292 }
293
294 int
295 flib_init(void)
296 {
297         if ((core_cfg = rte_zmalloc("core_cfg",
298                 sizeof(struct lcore_stat) * RTE_MAX_LCORE,
299                 RTE_CACHE_LINE_SIZE)) == NULL ) {
300                 printf("rte_zmalloc failed\n");
301                 return -1;
302         }
303
304         if (lcore_id_init() != 0) {
305                 printf("lcore_id_init failed\n");
306                 return -1;
307         }
308
309         return 0;
310 }