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