929fbc36742548e45ddaf9b637800ac43f6c2439
[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_log.h>
44
45 #include "fd_man.h"
46
47 /**
48  * Returns the index in the fdset for a given fd.
49  * If fd is -1, it means to search for a free entry.
50  * @return
51  *   index for the fd, or -1 if fd isn't in the fdset.
52  */
53 static int
54 fdset_find_fd(struct fdset *pfdset, int fd)
55 {
56         int i;
57
58         if (pfdset == NULL)
59                 return -1;
60
61         for (i = 0; i < MAX_FDS && pfdset->fd[i].fd != fd; i++)
62                 ;
63
64         return i ==  MAX_FDS ? -1 : i;
65 }
66
67 static int
68 fdset_find_free_slot(struct fdset *pfdset)
69 {
70         return fdset_find_fd(pfdset, -1);
71 }
72
73 static void
74 fdset_add_fd(struct fdset  *pfdset, int idx, int fd,
75         fd_cb rcb, fd_cb wcb, void *dat)
76 {
77         struct fdentry *pfdentry;
78
79         if (pfdset == NULL || idx >= MAX_FDS)
80                 return;
81
82         pfdentry = &pfdset->fd[idx];
83         pfdentry->fd = fd;
84         pfdentry->rcb = rcb;
85         pfdentry->wcb = wcb;
86         pfdentry->dat = dat;
87 }
88
89 /**
90  * Fill the read/write fd_set with the fds in the fdset.
91  * @return
92  *  the maximum fds filled in the read/write fd_set.
93  */
94 static int
95 fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
96 {
97         struct fdentry *pfdentry;
98         int i, maxfds = -1;
99         int num = MAX_FDS;
100
101         if (pfdset == NULL)
102                 return -1;
103
104         for (i = 0; i < num; i++) {
105                 pfdentry = &pfdset->fd[i];
106                 if (pfdentry->fd != -1) {
107                         int added = 0;
108                         if (pfdentry->rcb && rfset) {
109                                 FD_SET(pfdentry->fd, rfset);
110                                 added = 1;
111                         }
112                         if (pfdentry->wcb && wfset) {
113                                 FD_SET(pfdentry->fd, wfset);
114                                 added = 1;
115                         }
116                         if (added)
117                                 maxfds = pfdentry->fd < maxfds ?
118                                         maxfds : pfdentry->fd;
119                 }
120         }
121         return maxfds;
122 }
123
124 void
125 fdset_init(struct fdset *pfdset)
126 {
127         int i;
128
129         if (pfdset == NULL)
130                 return;
131
132         for (i = 0; i < MAX_FDS; i++)
133                 pfdset->fd[i].fd = -1;
134         pfdset->num = 0;
135 }
136
137 /**
138  * Register the fd in the fdset with read/write handler and context.
139  */
140 int
141 fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
142 {
143         int i;
144
145         if (pfdset == NULL || fd == -1)
146                 return -1;
147
148         /* Find a free slot in the list. */
149         i = fdset_find_free_slot(pfdset);
150         if (i == -1)
151                 return -2;
152
153         fdset_add_fd(pfdset, i, fd, rcb, wcb, dat);
154         pfdset->num++;
155
156         return 0;
157 }
158
159 /**
160  *  Unregister the fd from the fdset.
161  */
162 void
163 fdset_del(struct fdset *pfdset, int fd)
164 {
165         int i;
166
167         i = fdset_find_fd(pfdset, fd);
168         if (i != -1 && fd != -1) {
169                 pfdset->fd[i].fd = -1;
170                 pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
171                 pfdset->num--;
172         }
173 }
174
175 /**
176  * This functions runs in infinite blocking loop until there is no fd in
177  * pfdset. It calls corresponding r/w handler if there is event on the fd.
178  */
179 void
180 fdset_event_dispatch(struct fdset *pfdset)
181 {
182         fd_set rfds, wfds;
183         int i, maxfds;
184         struct fdentry *pfdentry;
185         int num = MAX_FDS;
186
187         if (pfdset == NULL)
188                 return;
189
190         while (1) {
191                 FD_ZERO(&rfds);
192                 FD_ZERO(&wfds);
193                 maxfds = fdset_fill(&rfds, &wfds, pfdset);
194                 if (maxfds == -1)
195                         return;
196
197                 select(maxfds + 1, &rfds, &wfds, NULL, NULL);
198
199                 for (i = 0; i < num; i++) {
200                         pfdentry = &pfdset->fd[i];
201                         if (pfdentry->fd >= 0 && FD_ISSET(pfdentry->fd, &rfds) && pfdentry->rcb)
202                                 pfdentry->rcb(pfdentry->fd, pfdentry->dat);
203                         if (pfdentry->fd >= 0 && FD_ISSET(pfdentry->fd, &wfds) && pfdentry->wcb)
204                                 pfdentry->wcb(pfdentry->fd, pfdentry->dat);
205                 }
206         }
207 }