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