]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/lib/librte_vhost/fd_man.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <sys/socket.h>
10 #include <sys/types.h>
14 #include <rte_common.h>
20 #define RTE_LOGTYPE_VHOST_FDMAN RTE_LOGTYPE_USER1
22 #define FDPOLLERR (POLLERR | POLLHUP | POLLNVAL)
25 get_last_valid_idx(struct fdset
*pfdset
, int last_valid_idx
)
29 for (i
= last_valid_idx
; i
>= 0 && pfdset
->fd
[i
].fd
== -1; i
--)
36 fdset_move(struct fdset
*pfdset
, int dst
, int src
)
38 pfdset
->fd
[dst
] = pfdset
->fd
[src
];
39 pfdset
->rwfds
[dst
] = pfdset
->rwfds
[src
];
43 fdset_shrink_nolock(struct fdset
*pfdset
)
46 int last_valid_idx
= get_last_valid_idx(pfdset
, pfdset
->num
- 1);
48 for (i
= 0; i
< last_valid_idx
; i
++) {
49 if (pfdset
->fd
[i
].fd
!= -1)
52 fdset_move(pfdset
, i
, last_valid_idx
);
53 last_valid_idx
= get_last_valid_idx(pfdset
, last_valid_idx
- 1);
55 pfdset
->num
= last_valid_idx
+ 1;
59 * Find deleted fd entries and remove them
62 fdset_shrink(struct fdset
*pfdset
)
64 pthread_mutex_lock(&pfdset
->fd_mutex
);
65 fdset_shrink_nolock(pfdset
);
66 pthread_mutex_unlock(&pfdset
->fd_mutex
);
70 * Returns the index in the fdset for a given fd.
72 * index for the fd, or -1 if fd isn't in the fdset.
75 fdset_find_fd(struct fdset
*pfdset
, int fd
)
79 for (i
= 0; i
< pfdset
->num
&& pfdset
->fd
[i
].fd
!= fd
; i
++)
82 return i
== pfdset
->num
? -1 : i
;
86 fdset_add_fd(struct fdset
*pfdset
, int idx
, int fd
,
87 fd_cb rcb
, fd_cb wcb
, void *dat
)
89 struct fdentry
*pfdentry
= &pfdset
->fd
[idx
];
90 struct pollfd
*pfd
= &pfdset
->rwfds
[idx
];
98 pfd
->events
= rcb
? POLLIN
: 0;
99 pfd
->events
|= wcb
? POLLOUT
: 0;
104 fdset_init(struct fdset
*pfdset
)
111 for (i
= 0; i
< MAX_FDS
; i
++) {
112 pfdset
->fd
[i
].fd
= -1;
113 pfdset
->fd
[i
].dat
= NULL
;
119 * Register the fd in the fdset with read/write handler and context.
122 fdset_add(struct fdset
*pfdset
, int fd
, fd_cb rcb
, fd_cb wcb
, void *dat
)
126 if (pfdset
== NULL
|| fd
== -1)
129 pthread_mutex_lock(&pfdset
->fd_mutex
);
130 i
= pfdset
->num
< MAX_FDS
? pfdset
->num
++ : -1;
132 pthread_mutex_lock(&pfdset
->fd_pooling_mutex
);
133 fdset_shrink_nolock(pfdset
);
134 pthread_mutex_unlock(&pfdset
->fd_pooling_mutex
);
135 i
= pfdset
->num
< MAX_FDS
? pfdset
->num
++ : -1;
137 pthread_mutex_unlock(&pfdset
->fd_mutex
);
142 fdset_add_fd(pfdset
, i
, fd
, rcb
, wcb
, dat
);
143 pthread_mutex_unlock(&pfdset
->fd_mutex
);
149 * Unregister the fd from the fdset.
150 * Returns context of a given fd or NULL.
153 fdset_del(struct fdset
*pfdset
, int fd
)
158 if (pfdset
== NULL
|| fd
== -1)
162 pthread_mutex_lock(&pfdset
->fd_mutex
);
164 i
= fdset_find_fd(pfdset
, fd
);
165 if (i
!= -1 && pfdset
->fd
[i
].busy
== 0) {
166 /* busy indicates r/wcb is executing! */
167 dat
= pfdset
->fd
[i
].dat
;
168 pfdset
->fd
[i
].fd
= -1;
169 pfdset
->fd
[i
].rcb
= pfdset
->fd
[i
].wcb
= NULL
;
170 pfdset
->fd
[i
].dat
= NULL
;
173 pthread_mutex_unlock(&pfdset
->fd_mutex
);
180 * Unregister the fd from the fdset.
182 * If parameters are invalid, return directly -2.
183 * And check whether fd is busy, if yes, return -1.
184 * Otherwise, try to delete the fd from fdset and
188 fdset_try_del(struct fdset
*pfdset
, int fd
)
192 if (pfdset
== NULL
|| fd
== -1)
195 pthread_mutex_lock(&pfdset
->fd_mutex
);
196 i
= fdset_find_fd(pfdset
, fd
);
197 if (i
!= -1 && pfdset
->fd
[i
].busy
) {
198 pthread_mutex_unlock(&pfdset
->fd_mutex
);
203 pfdset
->fd
[i
].fd
= -1;
204 pfdset
->fd
[i
].rcb
= pfdset
->fd
[i
].wcb
= NULL
;
205 pfdset
->fd
[i
].dat
= NULL
;
208 pthread_mutex_unlock(&pfdset
->fd_mutex
);
213 * This functions runs in infinite blocking loop until there is no fd in
214 * pfdset. It calls corresponding r/w handler if there is event on the fd.
216 * Before the callback is called, we set the flag to busy status; If other
217 * thread(now rte_vhost_driver_unregister) calls fdset_del concurrently, it
218 * will wait until the flag is reset to zero(which indicates the callback is
219 * finished), then it could free the context after fdset_del.
222 fdset_event_dispatch(void *arg
)
226 struct fdentry
*pfdentry
;
230 int remove1
, remove2
;
232 struct fdset
*pfdset
= arg
;
241 * When poll is blocked, other threads might unregister
242 * listenfds from and register new listenfds into fdset.
243 * When poll returns, the entries for listenfds in the fdset
244 * might have been updated. It is ok if there is unwanted call
247 pthread_mutex_lock(&pfdset
->fd_mutex
);
248 numfds
= pfdset
->num
;
249 pthread_mutex_unlock(&pfdset
->fd_mutex
);
251 pthread_mutex_lock(&pfdset
->fd_pooling_mutex
);
252 val
= poll(pfdset
->rwfds
, numfds
, 1000 /* millisecs */);
253 pthread_mutex_unlock(&pfdset
->fd_pooling_mutex
);
258 for (i
= 0; i
< numfds
; i
++) {
259 pthread_mutex_lock(&pfdset
->fd_mutex
);
261 pfdentry
= &pfdset
->fd
[i
];
263 pfd
= &pfdset
->rwfds
[i
];
267 pthread_mutex_unlock(&pfdset
->fd_mutex
);
272 pthread_mutex_unlock(&pfdset
->fd_mutex
);
276 remove1
= remove2
= 0;
283 pthread_mutex_unlock(&pfdset
->fd_mutex
);
285 if (rcb
&& pfd
->revents
& (POLLIN
| FDPOLLERR
))
286 rcb(fd
, dat
, &remove1
);
287 if (wcb
&& pfd
->revents
& (POLLOUT
| FDPOLLERR
))
288 wcb(fd
, dat
, &remove2
);
291 * fdset_del needs to check busy flag.
292 * We don't allow fdset_del to be called in callback
296 * When we are to clean up the fd from fdset,
297 * because the fd is closed in the cb,
298 * the old fd val could be reused by when creates new
299 * listen fd in another thread, we couldn't call
302 if (remove1
|| remove2
) {
309 fdset_shrink(pfdset
);
316 fdset_pipe_read_cb(int readfd
, void *dat __rte_unused
,
317 int *remove __rte_unused
)
320 int r
= read(readfd
, charbuf
, sizeof(charbuf
));
322 * Just an optimization, we don't care if read() failed
323 * so ignore explicitly its return value to make the
330 fdset_pipe_uninit(struct fdset
*fdset
)
332 fdset_del(fdset
, fdset
->u
.readfd
);
333 close(fdset
->u
.readfd
);
334 close(fdset
->u
.writefd
);
338 fdset_pipe_init(struct fdset
*fdset
)
342 if (pipe(fdset
->u
.pipefd
) < 0) {
343 RTE_LOG(ERR
, VHOST_FDMAN
,
344 "failed to create pipe for vhost fdset\n");
348 ret
= fdset_add(fdset
, fdset
->u
.readfd
,
349 fdset_pipe_read_cb
, NULL
, NULL
);
352 RTE_LOG(ERR
, VHOST_FDMAN
,
353 "failed to add pipe readfd %d into vhost server fdset\n",
356 fdset_pipe_uninit(fdset
);
364 fdset_pipe_notify(struct fdset
*fdset
)
366 int r
= write(fdset
->u
.writefd
, "1", 1);
368 * Just an optimization, we don't care if write() failed
369 * so ignore explicitly its return value to make the