2015-02-23 17:36:30 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
#include <rte_common.h>
|
2015-02-23 17:36:30 +00:00
|
|
|
#include <rte_log.h>
|
|
|
|
|
|
|
|
#include "fd_man.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the index in the fdset for a given fd.
|
|
|
|
* If fd is -1, it means to search for a free entry.
|
|
|
|
* @return
|
|
|
|
* index for the fd, or -1 if fd isn't in the fdset.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fdset_find_fd(struct fdset *pfdset, int fd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (pfdset == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_FDS && pfdset->fd[i].fd != fd; i++)
|
|
|
|
;
|
|
|
|
|
|
|
|
return i == MAX_FDS ? -1 : i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fdset_find_free_slot(struct fdset *pfdset)
|
|
|
|
{
|
|
|
|
return fdset_find_fd(pfdset, -1);
|
|
|
|
}
|
|
|
|
|
2016-07-06 14:24:58 +02:00
|
|
|
static int
|
2015-02-23 17:36:30 +00:00
|
|
|
fdset_add_fd(struct fdset *pfdset, int idx, int fd,
|
|
|
|
fd_cb rcb, fd_cb wcb, void *dat)
|
|
|
|
{
|
|
|
|
struct fdentry *pfdentry;
|
|
|
|
|
2016-07-06 14:24:58 +02:00
|
|
|
if (pfdset == NULL || idx >= MAX_FDS || fd >= FD_SETSIZE)
|
|
|
|
return -1;
|
2015-02-23 17:36:30 +00:00
|
|
|
|
|
|
|
pfdentry = &pfdset->fd[idx];
|
|
|
|
pfdentry->fd = fd;
|
|
|
|
pfdentry->rcb = rcb;
|
|
|
|
pfdentry->wcb = wcb;
|
|
|
|
pfdentry->dat = dat;
|
2016-07-06 14:24:58 +02:00
|
|
|
|
|
|
|
return 0;
|
2015-02-23 17:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill the read/write fd_set with the fds in the fdset.
|
|
|
|
* @return
|
|
|
|
* the maximum fds filled in the read/write fd_set.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fdset_fill(fd_set *rfset, fd_set *wfset, struct fdset *pfdset)
|
|
|
|
{
|
|
|
|
struct fdentry *pfdentry;
|
|
|
|
int i, maxfds = -1;
|
|
|
|
int num = MAX_FDS;
|
|
|
|
|
|
|
|
if (pfdset == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
pfdentry = &pfdset->fd[i];
|
|
|
|
if (pfdentry->fd != -1) {
|
|
|
|
int added = 0;
|
|
|
|
if (pfdentry->rcb && rfset) {
|
|
|
|
FD_SET(pfdentry->fd, rfset);
|
|
|
|
added = 1;
|
|
|
|
}
|
|
|
|
if (pfdentry->wcb && wfset) {
|
|
|
|
FD_SET(pfdentry->fd, wfset);
|
|
|
|
added = 1;
|
|
|
|
}
|
|
|
|
if (added)
|
|
|
|
maxfds = pfdentry->fd < maxfds ?
|
|
|
|
maxfds : pfdentry->fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxfds;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fdset_init(struct fdset *pfdset)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (pfdset == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_FDS; i++)
|
|
|
|
pfdset->fd[i].fd = -1;
|
|
|
|
pfdset->num = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the fd in the fdset with read/write handler and context.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (pfdset == NULL || fd == -1)
|
|
|
|
return -1;
|
|
|
|
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
pthread_mutex_lock(&pfdset->fd_mutex);
|
|
|
|
|
2015-02-23 17:36:30 +00:00
|
|
|
/* Find a free slot in the list. */
|
|
|
|
i = fdset_find_free_slot(pfdset);
|
2016-07-06 14:24:58 +02:00
|
|
|
if (i == -1 || fdset_add_fd(pfdset, i, fd, rcb, wcb, dat) < 0) {
|
2015-12-11 01:57:19 +08:00
|
|
|
pthread_mutex_unlock(&pfdset->fd_mutex);
|
2015-02-23 17:36:30 +00:00
|
|
|
return -2;
|
2015-12-11 01:57:19 +08:00
|
|
|
}
|
2015-02-23 17:36:30 +00:00
|
|
|
|
|
|
|
pfdset->num++;
|
|
|
|
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
pthread_mutex_unlock(&pfdset->fd_mutex);
|
|
|
|
|
2015-02-23 17:36:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister the fd from the fdset.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
fdset_del(struct fdset *pfdset, int fd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
if (pfdset == NULL || fd == -1)
|
|
|
|
return;
|
|
|
|
|
2015-03-06 13:52:43 +08:00
|
|
|
do {
|
|
|
|
pthread_mutex_lock(&pfdset->fd_mutex);
|
|
|
|
|
|
|
|
i = fdset_find_fd(pfdset, fd);
|
|
|
|
if (i != -1 && pfdset->fd[i].busy == 0) {
|
|
|
|
/* busy indicates r/wcb is executing! */
|
|
|
|
pfdset->fd[i].fd = -1;
|
|
|
|
pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL;
|
|
|
|
pfdset->num--;
|
|
|
|
i = -1;
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
}
|
2015-03-06 13:52:43 +08:00
|
|
|
pthread_mutex_unlock(&pfdset->fd_mutex);
|
|
|
|
} while (i != -1);
|
2015-02-23 17:36:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 17:20:47 +08:00
|
|
|
/**
|
|
|
|
* Unregister the fd at the specified slot from the fdset.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fdset_del_slot(struct fdset *pfdset, int index)
|
|
|
|
{
|
|
|
|
if (pfdset == NULL || index < 0 || index >= MAX_FDS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&pfdset->fd_mutex);
|
|
|
|
|
|
|
|
pfdset->fd[index].fd = -1;
|
|
|
|
pfdset->fd[index].rcb = pfdset->fd[index].wcb = NULL;
|
|
|
|
pfdset->num--;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&pfdset->fd_mutex);
|
|
|
|
}
|
|
|
|
|
2015-02-23 17:36:30 +00:00
|
|
|
/**
|
|
|
|
* This functions runs in infinite blocking loop until there is no fd in
|
|
|
|
* pfdset. It calls corresponding r/w handler if there is event on the fd.
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
*
|
|
|
|
* Before the callback is called, we set the flag to busy status; If other
|
|
|
|
* thread(now rte_vhost_driver_unregister) calls fdset_del concurrently, it
|
|
|
|
* will wait until the flag is reset to zero(which indicates the callback is
|
|
|
|
* finished), then it could free the context after fdset_del.
|
2015-02-23 17:36:30 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
fdset_event_dispatch(struct fdset *pfdset)
|
|
|
|
{
|
|
|
|
fd_set rfds, wfds;
|
|
|
|
int i, maxfds;
|
|
|
|
struct fdentry *pfdentry;
|
|
|
|
int num = MAX_FDS;
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
fd_cb rcb, wcb;
|
|
|
|
void *dat;
|
|
|
|
int fd;
|
|
|
|
int remove1, remove2;
|
2015-03-06 13:52:27 +08:00
|
|
|
int ret;
|
2015-02-23 17:36:30 +00:00
|
|
|
|
|
|
|
if (pfdset == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (1) {
|
2015-03-06 13:52:27 +08:00
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
2015-02-23 17:36:30 +00:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&wfds);
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
pthread_mutex_lock(&pfdset->fd_mutex);
|
|
|
|
|
2015-02-23 17:36:30 +00:00
|
|
|
maxfds = fdset_fill(&rfds, &wfds, pfdset);
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&pfdset->fd_mutex);
|
2015-02-23 17:36:30 +00:00
|
|
|
|
2015-06-30 17:20:50 +08:00
|
|
|
/*
|
|
|
|
* When select is blocked, other threads might unregister
|
|
|
|
* listenfds from and register new listenfds into fdset.
|
|
|
|
* When select returns, the entries for listenfds in the fdset
|
|
|
|
* might have been updated. It is ok if there is unwanted call
|
|
|
|
* for new listenfds.
|
|
|
|
*/
|
2015-03-06 13:52:27 +08:00
|
|
|
ret = select(maxfds + 1, &rfds, &wfds, NULL, &tv);
|
|
|
|
if (ret <= 0)
|
|
|
|
continue;
|
2015-02-23 17:36:30 +00:00
|
|
|
|
|
|
|
for (i = 0; i < num; i++) {
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
remove1 = remove2 = 0;
|
|
|
|
pthread_mutex_lock(&pfdset->fd_mutex);
|
2015-02-23 17:36:30 +00:00
|
|
|
pfdentry = &pfdset->fd[i];
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
fd = pfdentry->fd;
|
|
|
|
rcb = pfdentry->rcb;
|
|
|
|
wcb = pfdentry->wcb;
|
|
|
|
dat = pfdentry->dat;
|
|
|
|
pfdentry->busy = 1;
|
|
|
|
pthread_mutex_unlock(&pfdset->fd_mutex);
|
|
|
|
if (fd >= 0 && FD_ISSET(fd, &rfds) && rcb)
|
|
|
|
rcb(fd, dat, &remove1);
|
|
|
|
if (fd >= 0 && FD_ISSET(fd, &wfds) && wcb)
|
|
|
|
wcb(fd, dat, &remove2);
|
|
|
|
pfdentry->busy = 0;
|
|
|
|
/*
|
|
|
|
* fdset_del needs to check busy flag.
|
|
|
|
* We don't allow fdset_del to be called in callback
|
|
|
|
* directly.
|
|
|
|
*/
|
2015-06-30 17:20:47 +08:00
|
|
|
/*
|
|
|
|
* When we are to clean up the fd from fdset,
|
|
|
|
* because the fd is closed in the cb,
|
|
|
|
* the old fd val could be reused by when creates new
|
|
|
|
* listen fd in another thread, we couldn't call
|
|
|
|
* fd_set_del.
|
|
|
|
*/
|
vhost: support dynamically registering server
* support calling rte_vhost_driver_register after rte_vhost_driver_session_start
* add mutext to protect fdset from concurrent access
* add busy flag in fdentry. this flag is set before cb and cleared after cb is finished.
mutex lock scenario in vhost:
* event_dispatch(in rte_vhost_driver_session_start) runs in a separate thread, infinitely
processing vhost messages through cb(callback).
* event_dispatch acquires the lock, get the cb and its context, mark the busy flag,
and releases the mutex.
* vserver_new_vq_conn cb calls fdset_add, which acquires the mutex and add new fd into fdset.
* vserver_message_handler cb frees data context, marks remove flag to request to delete
connfd(connection fd) from fdset.
* after cb returns, event_dispatch
1. clears busy flag.
2. if there is remove request, call fdset_del, which acquires mutex, checks busy flag, and
removes connfd from fdset.
* rte_vhost_driver_unregister(not implemented) runs in another thread, acquires the mutex,
calls fdset_del to remove fd(listenerfd) from fdset. Then it could free data context.
The above steps ensures fd data context isn't freed when cb is using.
VM(s) should have been shutdown before rte_vhost_driver_unregister.
Signed-off-by: Huawei Xie <huawei.xie@intel.com>
Acked-by: Konstantin Ananyev <konstantin.ananyev@intel.com>
Acked-by: Tetsuya Mukawa <mukawa@igel.co.jp>
2015-02-23 17:36:33 +00:00
|
|
|
if (remove1 || remove2)
|
2015-06-30 17:20:47 +08:00
|
|
|
fdset_del_slot(pfdset, i);
|
2015-02-23 17:36:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|