vhost: fix race for connection fd
[dpdk.git] / lib / librte_vhost / vhost_user / fd_man.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 <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sys/socket.h>
38 #include <sys/select.h>
39 #include <sys/time.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42
43 #include <rte_common.h>
44 #include <rte_log.h>
45
46 #include "fd_man.h"
47
48 /**
49  * Returns the index in the fdset for a given fd.
50  * If fd is -1, it means to search for a free entry.
51  * @return
52  *   index for the fd, or -1 if fd isn't in the fdset.
53  */
54 static int
55 fdset_find_fd(struct fdset *pfdset, int fd)
56 {
57         int i;
58
59         if (pfdset == NULL)
60                 return -1;
61
62         for (i = 0; i < MAX_FDS && pfdset->fd[i].fd != fd; i++)
63                 ;
64
65         return i ==  MAX_FDS ? -1 : i;
66 }
67
68 static int
69 fdset_find_free_slot(struct fdset *pfdset)
70 {
71         return fdset_find_fd(pfdset, -1);
72 }
73
74 static void
75 fdset_add_fd(struct fdset  *pfdset, int idx, int fd,
76         fd_cb rcb, fd_cb wcb, void *dat)
77 {
78         struct fdentry *pfdentry;
79
80         if (pfdset == NULL || idx >= MAX_FDS)
81                 return;
82
83         pfdentry = &pfdset->fd[idx];
84         pfdentry->fd = fd;
85         pfdentry->rcb = rcb;
86         pfdentry->wcb = wcb;
87         pfdentry->dat = dat;
88 }
89
90 /**
91  * Fill the read/write fd_set with the fds in the fdset.
92  * @return
93  *  the maximum fds filled in the read/write fd_set.
94  */
95 static int
96 fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
97 {
98         struct fdentry *pfdentry;
99         int i, maxfds = -1;
100         int num = MAX_FDS;
101
102         if (pfdset == NULL)
103                 return -1;
104
105         for (i = 0; i < num; i++) {
106                 pfdentry = &pfdset->fd[i];
107                 if (pfdentry->fd != -1) {
108                         int added = 0;
109                         if (pfdentry->rcb && rfset) {
110                                 FD_SET(pfdentry->fd, rfset);
111                                 added = 1;
112                         }
113                         if (pfdentry->wcb && wfset) {
114                                 FD_SET(pfdentry->fd, wfset);
115                                 added = 1;
116                         }
117                         if (added)
118                                 maxfds = pfdentry->fd < maxfds ?
119                                         maxfds : pfdentry->fd;
120                 }
121         }
122         return maxfds;
123 }
124
125 void
126 fdset_init(struct fdset *pfdset)
127 {
128         int i;
129
130         if (pfdset == NULL)
131                 return;
132
133         for (i = 0; i < MAX_FDS; i++)
134                 pfdset->fd[i].fd = -1;
135         pfdset->num = 0;
136 }
137
138 /**
139  * Register the fd in the fdset with read/write handler and context.
140  */
141 int
142 fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
143 {
144         int i;
145
146         if (pfdset == NULL || fd == -1)
147                 return -1;
148
149         pthread_mutex_lock(&pfdset->fd_mutex);
150
151         /* Find a free slot in the list. */
152         i = fdset_find_free_slot(pfdset);
153         if (i == -1)
154                 return -2;
155
156         fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
157         pfdset->num++;
158
159         pthread_mutex_unlock(&pfdset->fd_mutex);
160
161         return 0;
162 }
163
164 /**
165  *  Unregister the fd from the fdset.
166  */
167 void
168 fdset_del(struct fdset *pfdset, int fd)
169 {
170         int i;
171
172         if (pfdset == NULL || fd == -1)
173                 return;
174
175         do {
176                 pthread_mutex_lock(&pfdset->fd_mutex);
177
178                 i = fdset_find_fd(pfdset, fd);
179                 if (i != -1 && pfdset->fd[i].busy == 0) {
180                         /* busy indicates r/wcb is executing! */
181                         pfdset->fd[i].fd = -1;
182                         pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
183                         pfdset->num--;
184                         i = -1;
185                 }
186                 pthread_mutex_unlock(&pfdset->fd_mutex);
187         } while (i != -1);
188 }
189
190 /**
191  *  Unregister the fd at the specified slot from the fdset.
192  */
193 static void
194 fdset_del_slot(struct fdset *pfdset, int index)
195 {
196         if (pfdset == NULL || index < 0 || index >= MAX_FDS)
197                 return;
198
199         pthread_mutex_lock(&pfdset->fd_mutex);
200
201         pfdset->fd[index].fd = -1;
202         pfdset->fd[index].rcb = pfdset->fd[index].wcb = NULL;
203         pfdset->num--;
204
205         pthread_mutex_unlock(&pfdset->fd_mutex);
206 }
207
208 /**
209  * This functions runs in infinite blocking loop until there is no fd in
210  * pfdset. It calls corresponding r/w handler if there is event on the fd.
211  *
212  * Before the callback is called, we set the flag to busy status; If other
213  * thread(now rte_vhost_driver_unregister) calls fdset_del concurrently, it
214  * will wait until the flag is reset to zero(which indicates the callback is
215  * finished), then it could free the context after fdset_del.
216  */
217 void
218 fdset_event_dispatch(struct fdset *pfdset)
219 {
220         fd_set rfds, wfds;
221         int i, maxfds;
222         struct fdentry *pfdentry;
223         int num = MAX_FDS;
224         fd_cb rcb, wcb;
225         void *dat;
226         int fd;
227         int remove1, remove2;
228         int ret;
229
230         if (pfdset == NULL)
231                 return;
232
233         while (1) {
234                 struct timeval tv;
235                 tv.tv_sec = 1;
236                 tv.tv_usec = 0;
237                 FD_ZERO(&rfds);
238                 FD_ZERO(&wfds);
239                 pthread_mutex_lock(&pfdset->fd_mutex);
240
241                 maxfds = fdset_fill(&rfds, &wfds, pfdset);
242
243                 pthread_mutex_unlock(&pfdset->fd_mutex);
244
245                 ret = select(maxfds + 1, &rfds, &wfds, NULL, &tv);
246                 if (ret <= 0)
247                         continue;
248
249                 for (i = 0; i < num; i++) {
250                         remove1 = remove2 = 0;
251                         pthread_mutex_lock(&pfdset->fd_mutex);
252                         pfdentry = &pfdset->fd[i];
253                         fd = pfdentry->fd;
254                         rcb = pfdentry->rcb;
255                         wcb = pfdentry->wcb;
256                         dat = pfdentry->dat;
257                         pfdentry->busy = 1;
258                         pthread_mutex_unlock(&pfdset->fd_mutex);
259                         if (fd >= 0 && FD_ISSET(fd, &rfds) && rcb)
260                                 rcb(fd, dat, &remove1);
261                         if (fd >= 0 && FD_ISSET(fd, &wfds) && wcb)
262                                 wcb(fd, dat, &remove2);
263                         pfdentry->busy = 0;
264                         /*
265                          * fdset_del needs to check busy flag.
266                          * We don't allow fdset_del to be called in callback
267                          * directly.
268                          */
269                         /*
270                          * When we are to clean up the fd from fdset,
271                          * because the fd is closed in the cb,
272                          * the old fd val could be reused by when creates new
273                          * listen fd in another thread, we couldn't call
274                          * fd_set_del.
275                          */
276                         if (remove1 || remove2)
277                                 fdset_del_slot(pfdset, i);
278                 }
279         }
280 }