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