Imported Upstream version 16.11
[deb_dpdk.git] / lib / librte_vhost / 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 int
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 || fd >= FD_SETSIZE)
81                 return -1;
82
83         pfdentry = &pfdset->fd[idx];
84         pfdentry->fd = fd;
85         pfdentry->rcb = rcb;
86         pfdentry->wcb = wcb;
87         pfdentry->dat = dat;
88
89         return 0;
90 }
91
92 /**
93  * Fill the read/write fd_set with the fds in the fdset.
94  * @return
95  *  the maximum fds filled in the read/write fd_set.
96  */
97 static int
98 fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
99 {
100         struct fdentry *pfdentry;
101         int i, maxfds = -1;
102         int num = MAX_FDS;
103
104         if (pfdset == NULL)
105                 return -1;
106
107         for (i = 0; i < num; i++) {
108                 pfdentry = &pfdset->fd[i];
109                 if (pfdentry->fd != -1) {
110                         int added = 0;
111                         if (pfdentry->rcb && rfset) {
112                                 FD_SET(pfdentry->fd, rfset);
113                                 added = 1;
114                         }
115                         if (pfdentry->wcb && wfset) {
116                                 FD_SET(pfdentry->fd, wfset);
117                                 added = 1;
118                         }
119                         if (added)
120                                 maxfds = pfdentry->fd < maxfds ?
121                                         maxfds : pfdentry->fd;
122                 }
123         }
124         return maxfds;
125 }
126
127 void
128 fdset_init(struct fdset *pfdset)
129 {
130         int i;
131
132         if (pfdset == NULL)
133                 return;
134
135         for (i = 0; i < MAX_FDS; i++) {
136                 pfdset->fd[i].fd = -1;
137                 pfdset->fd[i].dat = NULL;
138         }
139         pfdset->num = 0;
140 }
141
142 /**
143  * Register the fd in the fdset with read/write handler and context.
144  */
145 int
146 fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
147 {
148         int i;
149
150         if (pfdset == NULL || fd == -1)
151                 return -1;
152
153         pthread_mutex_lock(&pfdset->fd_mutex);
154
155         /* Find a free slot in the list. */
156         i = fdset_find_free_slot(pfdset);
157         if (i == -1 || fdset_add_fd(pfdset, i, fd, rcb, wcb, dat) < 0) {
158                 pthread_mutex_unlock(&pfdset->fd_mutex);
159                 return -2;
160         }
161
162         pfdset->num++;
163
164         pthread_mutex_unlock(&pfdset->fd_mutex);
165
166         return 0;
167 }
168
169 /**
170  *  Unregister the fd from the fdset.
171  *  Returns context of a given fd or NULL.
172  */
173 void *
174 fdset_del(struct fdset *pfdset, int fd)
175 {
176         int i;
177         void *dat = NULL;
178
179         if (pfdset == NULL || fd == -1)
180                 return NULL;
181
182         do {
183                 pthread_mutex_lock(&pfdset->fd_mutex);
184
185                 i = fdset_find_fd(pfdset, fd);
186                 if (i != -1 && pfdset->fd[i].busy == 0) {
187                         /* busy indicates r/wcb is executing! */
188                         dat = pfdset->fd[i].dat;
189                         pfdset->fd[i].fd = -1;
190                         pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
191                         pfdset->fd[i].dat = NULL;
192                         pfdset->num--;
193                         i = -1;
194                 }
195                 pthread_mutex_unlock(&pfdset->fd_mutex);
196         } while (i != -1);
197
198         return dat;
199 }
200
201 /**
202  *  Unregister the fd at the specified slot from the fdset.
203  */
204 static void
205 fdset_del_slot(struct fdset *pfdset, int index)
206 {
207         if (pfdset == NULL || index < 0 || index >= MAX_FDS)
208                 return;
209
210         pthread_mutex_lock(&pfdset->fd_mutex);
211
212         pfdset->fd[index].fd = -1;
213         pfdset->fd[index].rcb = pfdset->fd[index].wcb = NULL;
214         pfdset->fd[index].dat = NULL;
215         pfdset->num--;
216
217         pthread_mutex_unlock(&pfdset->fd_mutex);
218 }
219
220 /**
221  * This functions runs in infinite blocking loop until there is no fd in
222  * pfdset. It calls corresponding r/w handler if there is event on the fd.
223  *
224  * Before the callback is called, we set the flag to busy status; If other
225  * thread(now rte_vhost_driver_unregister) calls fdset_del concurrently, it
226  * will wait until the flag is reset to zero(which indicates the callback is
227  * finished), then it could free the context after fdset_del.
228  */
229 void
230 fdset_event_dispatch(struct fdset *pfdset)
231 {
232         fd_set rfds, wfds;
233         int i, maxfds;
234         struct fdentry *pfdentry;
235         int num = MAX_FDS;
236         fd_cb rcb, wcb;
237         void *dat;
238         int fd;
239         int remove1, remove2;
240         int ret;
241
242         if (pfdset == NULL)
243                 return;
244
245         while (1) {
246                 struct timeval tv;
247                 tv.tv_sec = 1;
248                 tv.tv_usec = 0;
249                 FD_ZERO(&rfds);
250                 FD_ZERO(&wfds);
251                 pthread_mutex_lock(&pfdset->fd_mutex);
252
253                 maxfds = fdset_fill(&rfds, &wfds, pfdset);
254
255                 pthread_mutex_unlock(&pfdset->fd_mutex);
256
257                 /*
258                  * When select is blocked, other threads might unregister
259                  * listenfds from and register new listenfds into fdset.
260                  * When select returns, the entries for listenfds in the fdset
261                  * might have been updated. It is ok if there is unwanted call
262                  * for new listenfds.
263                  */
264                 ret = select(maxfds + 1, &rfds, &wfds, NULL, &tv);
265                 if (ret <= 0)
266                         continue;
267
268                 for (i = 0; i < num; i++) {
269                         remove1 = remove2 = 0;
270                         pthread_mutex_lock(&pfdset->fd_mutex);
271                         pfdentry = &pfdset->fd[i];
272                         fd = pfdentry->fd;
273                         rcb = pfdentry->rcb;
274                         wcb = pfdentry->wcb;
275                         dat = pfdentry->dat;
276                         pfdentry->busy = 1;
277                         pthread_mutex_unlock(&pfdset->fd_mutex);
278                         if (fd >= 0 && FD_ISSET(fd, &rfds) && rcb)
279                                 rcb(fd, dat, &remove1);
280                         if (fd >= 0 && FD_ISSET(fd, &wfds) && wcb)
281                                 wcb(fd, dat, &remove2);
282                         pfdentry->busy = 0;
283                         /*
284                          * fdset_del needs to check busy flag.
285                          * We don't allow fdset_del to be called in callback
286                          * directly.
287                          */
288                         /*
289                          * When we are to clean up the fd from fdset,
290                          * because the fd is closed in the cb,
291                          * the old fd val could be reused by when creates new
292                          * listen fd in another thread, we couldn't call
293                          * fd_set_del.
294                          */
295                         if (remove1 || remove2)
296                                 fdset_del_slot(pfdset, i);
297                 }
298         }
299 }