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