783d68c5c62232bb058aec56ee5b0a7313febddc
[deb_dpdk.git] / lib / librte_eal / bsdapp / eal / eal_thread.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
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdint.h>
38 #include <unistd.h>
39 #include <sched.h>
40 #include <pthread_np.h>
41 #include <sys/queue.h>
42 #include <sys/thr.h>
43
44 #include <rte_debug.h>
45 #include <rte_atomic.h>
46 #include <rte_launch.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_per_lcore.h>
51 #include <rte_eal.h>
52 #include <rte_lcore.h>
53
54 #include "eal_private.h"
55 #include "eal_thread.h"
56
57 RTE_DEFINE_PER_LCORE(unsigned, _lcore_id) = LCORE_ID_ANY;
58 RTE_DEFINE_PER_LCORE(unsigned, _socket_id) = (unsigned)SOCKET_ID_ANY;
59 RTE_DEFINE_PER_LCORE(rte_cpuset_t, _cpuset);
60
61 /*
62  * Send a message to a slave lcore identified by slave_id to call a
63  * function f with argument arg. Once the execution is done, the
64  * remote lcore switch in FINISHED state.
65  */
66 int
67 rte_eal_remote_launch(int (*f)(void *), void *arg, unsigned slave_id)
68 {
69         int n;
70         char c = 0;
71         int m2s = lcore_config[slave_id].pipe_master2slave[1];
72         int s2m = lcore_config[slave_id].pipe_slave2master[0];
73
74         if (lcore_config[slave_id].state != WAIT)
75                 return -EBUSY;
76
77         lcore_config[slave_id].f = f;
78         lcore_config[slave_id].arg = arg;
79
80         /* send message */
81         n = 0;
82         while (n == 0 || (n < 0 && errno == EINTR))
83                 n = write(m2s, &c, 1);
84         if (n < 0)
85                 rte_panic("cannot write on configuration pipe\n");
86
87         /* wait ack */
88         do {
89                 n = read(s2m, &c, 1);
90         } while (n < 0 && errno == EINTR);
91
92         if (n <= 0)
93                 rte_panic("cannot read on configuration pipe\n");
94
95         return 0;
96 }
97
98 /* set affinity for current thread */
99 static int
100 eal_thread_set_affinity(void)
101 {
102         unsigned lcore_id = rte_lcore_id();
103
104         /* acquire system unique id  */
105         rte_gettid();
106
107         /* update EAL thread core affinity */
108         return rte_thread_set_affinity(&lcore_config[lcore_id].cpuset);
109 }
110
111 void eal_thread_init_master(unsigned lcore_id)
112 {
113         /* set the lcore ID in per-lcore memory area */
114         RTE_PER_LCORE(_lcore_id) = lcore_id;
115
116         /* set CPU affinity */
117         if (eal_thread_set_affinity() < 0)
118                 rte_panic("cannot set affinity\n");
119 }
120
121 /* main loop of threads */
122 __attribute__((noreturn)) void *
123 eal_thread_loop(__attribute__((unused)) void *arg)
124 {
125         char c;
126         int n, ret;
127         unsigned lcore_id;
128         pthread_t thread_id;
129         int m2s, s2m;
130         char cpuset[RTE_CPU_AFFINITY_STR_LEN];
131
132         thread_id = pthread_self();
133
134         /* retrieve our lcore_id from the configuration structure */
135         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
136                 if (thread_id == lcore_config[lcore_id].thread_id)
137                         break;
138         }
139         if (lcore_id == RTE_MAX_LCORE)
140                 rte_panic("cannot retrieve lcore id\n");
141
142         m2s = lcore_config[lcore_id].pipe_master2slave[0];
143         s2m = lcore_config[lcore_id].pipe_slave2master[1];
144
145         /* set the lcore ID in per-lcore memory area */
146         RTE_PER_LCORE(_lcore_id) = lcore_id;
147
148         /* set CPU affinity */
149         if (eal_thread_set_affinity() < 0)
150                 rte_panic("cannot set affinity\n");
151
152         ret = eal_thread_dump_affinity(cpuset, RTE_CPU_AFFINITY_STR_LEN);
153
154         RTE_LOG(DEBUG, EAL, "lcore %u is ready (tid=%p;cpuset=[%s%s])\n",
155                 lcore_id, thread_id, cpuset, ret == 0 ? "" : "...");
156
157         /* read on our pipe to get commands */
158         while (1) {
159                 void *fct_arg;
160
161                 /* wait command */
162                 do {
163                         n = read(m2s, &c, 1);
164                 } while (n < 0 && errno == EINTR);
165
166                 if (n <= 0)
167                         rte_panic("cannot read on configuration pipe\n");
168
169                 lcore_config[lcore_id].state = RUNNING;
170
171                 /* send ack */
172                 n = 0;
173                 while (n == 0 || (n < 0 && errno == EINTR))
174                         n = write(s2m, &c, 1);
175                 if (n < 0)
176                         rte_panic("cannot write on configuration pipe\n");
177
178                 if (lcore_config[lcore_id].f == NULL)
179                         rte_panic("NULL function pointer\n");
180
181                 /* call the function and store the return value */
182                 fct_arg = lcore_config[lcore_id].arg;
183                 ret = lcore_config[lcore_id].f(fct_arg);
184                 lcore_config[lcore_id].ret = ret;
185                 rte_wmb();
186                 lcore_config[lcore_id].state = FINISHED;
187         }
188
189         /* never reached */
190         /* pthread_exit(NULL); */
191         /* return NULL; */
192 }
193
194 /* require calling thread tid by gettid() */
195 int rte_sys_gettid(void)
196 {
197         long lwpid;
198         thr_self(&lwpid);
199         return (int)lwpid;
200 }
201
202 int rte_thread_setname(pthread_t id, const char *name)
203 {
204         /* this BSD function returns no error */
205         pthread_set_name_np(id, name);
206         return 0;
207 }