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