From: Huawei Xie <huawei.xie@intel.com> To: dev@dpdk.org Cc: haifeng.lin@intel.com Subject: [dpdk-dev] [PATCH RFC v2 07/12] lib/librte_vhost: async event and callback Date: Thu, 11 Dec 2014 05:37:52 +0800 Message-ID: <1418247477-13920-8-git-send-email-huawei.xie@intel.com> (raw) In-Reply-To: <1418247477-13920-1-git-send-email-huawei.xie@intel.com> Signed-off-by: Huawei Xie <huawei.xie@intel.com> --- lib/librte_vhost/vhost_user/fd_man.c | 205 +++++++++++++++++++++++++++++++++++ lib/librte_vhost/vhost_user/fd_man.h | 64 +++++++++++ 2 files changed, 269 insertions(+) create mode 100644 lib/librte_vhost/vhost_user/fd_man.c create mode 100644 lib/librte_vhost/vhost_user/fd_man.h diff --git a/lib/librte_vhost/vhost_user/fd_man.c b/lib/librte_vhost/vhost_user/fd_man.c new file mode 100644 index 0000000..f4db68b --- /dev/null +++ b/lib/librte_vhost/vhost_user/fd_man.c @@ -0,0 +1,205 @@ +/*- + * 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> + +#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 < pfdset->num && pfdset->fd[i].fd != fd; i++); + + return i == pfdset->num ? -1 : i; +} + +static int +fdset_find_free_slot(struct fdset *pfdset) +{ + return fdset_find_fd(pfdset, -1); +} + +static void +fdset_add_fd(struct fdset *pfdset, int idx, int fd, + fd_cb rcb, fd_cb wcb, uint64_t dat) +{ + struct fdentry *pfdentry; + + if (pfdset == NULL || idx >= pfdset->num) + return; + + pfdentry = &pfdset->fd[idx]; + pfdentry->fd = fd; + pfdentry->rcb = rcb; + pfdentry->wcb = wcb; + pfdentry->dat = dat; +} + +/** + * Fill the read/write fdset 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 = MAX_FDS; +} + +/** + * 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, uint64_t dat) +{ + int i; + + if (pfdset == NULL || fd == -1) + return -1; + + /* Find a free slot in the list. */ + i = fdset_find_free_slot(pfdset); + if (i == -1) + return -2; + + fdset_add_fd(pfdset, i, fd, rcb, wcb, dat); + + return 0; +} + +/** + * Unregister the fd from the fdset. + */ +void +fdset_del(struct fdset *pfdset, int fd) +{ + int i; + + i = fdset_find_fd(pfdset, fd); + if (i != -1) { + pfdset->fd[i].fd = -1; + pfdset->fd[i].rcb = pfdset->fd[i].wcb = NULL; + } +} + +/** + * 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. + */ +void +fdset_event_dispatch(struct fdset *pfdset) +{ + fd_set rfds,wfds; + int i, maxfds; + struct fdentry *pfdentry; + int num = MAX_FDS; + + if (pfdset == NULL) + return; + + while (1) { + FD_ZERO(&rfds); + FD_ZERO(&wfds); + maxfds = fdset_fill(&rfds, &wfds, pfdset); + if (maxfds == -1) { + return; + } + + select(maxfds + 1, &rfds, &wfds, NULL, NULL); + + for (i = 0; i < num; i++) { + pfdentry = &pfdset->fd[i]; + if (FD_ISSET(pfdentry->fd, &rfds) && pfdentry->rcb) + pfdentry->rcb(pfdentry->fd, pfdentry->dat); + if (FD_ISSET(pfdentry->fd, &wfds) && pfdentry->wcb) + pfdentry->wcb(pfdentry->fd, pfdentry->dat); + } + } +} diff --git a/lib/librte_vhost/vhost_user/fd_man.h b/lib/librte_vhost/vhost_user/fd_man.h new file mode 100644 index 0000000..1722954 --- /dev/null +++ b/lib/librte_vhost/vhost_user/fd_man.h @@ -0,0 +1,64 @@ +/*- + * 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. + */ + +#ifndef _FD_MAN_H_ +#define _FD_MAN_H_ +#include <stdint.h> + +#define MAX_FDS 1024 + +typedef void (*fd_cb)(int fd, uint64_t dat); + +struct fdentry { + int fd; /* -1 indicates this entry is empty */ + fd_cb rcb; /* callback when this fd is readable. */ + fd_cb wcb; /* callback when this fd is writeable.*/ + uint64_t dat; /* fd context */ +}; + +struct fdset { + struct fdentry fd[MAX_FDS]; + int num; /* capacity of this fdset */ +}; + + +void fdset_init(struct fdset *pfdset); + +int fdset_add(struct fdset *pfdset, int fd, + fd_cb rcb, fd_cb wcb, uint64_t ctx); + +void fdset_del(struct fdset *pfdset, int fd); + +void fdset_event_dispatch(struct fdset *pfdset); + +#endif -- 1.8.1.4
next prev parent reply other threads:[~2014-12-10 21:38 UTC|newest] Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top 2014-12-10 21:37 [dpdk-dev] [PATCH RFC v2 00/12] lib/librte_vhost: vhost-user support Huawei Xie 2014-12-10 21:37 ` [dpdk-dev] [PATCH RFC v2 01/12] lib/librte_vhost: mov vhost-cuse implementation to vhost_cuse directory Huawei Xie 2014-12-10 21:37 ` [dpdk-dev] [PATCH RFC v2 02/12] lib/librte_vhost: rename vhost-net-cdev.h as vhost-net.h Huawei Xie 2014-12-10 21:37 ` [dpdk-dev] [PATCH RFC v2 03/12] lib/librte_vhost: move event_copy logic from virtio-net.c to vhost-net-cdev.c Huawei Xie 2015-01-07 9:10 ` Xie, Huawei 2014-12-10 21:37 ` [dpdk-dev] [PATCH RFC v2 04/12] lib/librte_vhost: copy of host_memory_map from virtio-net.c to new file virtio-net-cdev.c Huawei Xie 2014-12-10 21:37 ` [dpdk-dev] [PATCH RFC v2 05/12] lib/librte_vhost: host_memory_map refine Huawei Xie 2014-12-10 21:37 ` [dpdk-dev] [PATCH RFC v2 06/12] lib/librte_vhost: cuse_set_memory_table Huawei Xie 2014-12-15 5:20 ` Tetsuya Mukawa 2014-12-10 21:37 ` Huawei Xie [this message] 2014-12-15 5:20 ` [dpdk-dev] [PATCH RFC v2 07/12] lib/librte_vhost: async event and callback Tetsuya Mukawa 2014-12-17 17:51 ` Xie, Huawei 2014-12-10 21:37 ` [dpdk-dev] [PATCH RFC v2 08/12] lib/librte_vhost: vhost-user support Huawei Xie 2014-12-11 5:36 ` Linhaifeng 2015-01-05 10:21 ` Xie, Huawei 2015-01-23 3:40 ` Xie, Huawei 2015-01-23 3:53 ` Linhaifeng 2014-12-11 6:04 ` Linhaifeng 2014-12-11 17:13 ` Xie, Huawei 2014-12-12 2:25 ` Linhaifeng 2014-12-11 20:16 ` Xie, Huawei 2015-01-23 3:36 ` Xie, Huawei 2015-01-23 8:36 ` Linhaifeng 2014-12-16 3:05 ` Tetsuya Mukawa 2014-12-17 1:06 ` Xie, Huawei 2014-12-17 3:31 ` Tetsuya Mukawa 2014-12-17 4:22 ` Tetsuya Mukawa 2014-12-17 17:31 ` Xie, Huawei 2014-12-19 3:36 ` Tetsuya Mukawa 2014-12-24 7:21 ` Tetsuya Mukawa 2015-01-04 9:53 ` Xie, Huawei 2014-12-10 21:37 ` [dpdk-dev] [PATCH RFC v2 09/12] lib/librte_vhost: minor fix Huawei Xie 2014-12-10 21:37 ` [dpdk-dev] [PATCH RFC v2 10/12] lib/librte_vhost: vhost-user memory region map Huawei Xie 2014-12-16 2:38 ` Tetsuya Mukawa 2014-12-10 21:37 ` [dpdk-dev] [PATCH RFC v2 11/12] lib/librte_vhost: kick/callfd fix Huawei Xie 2014-12-10 21:37 ` [dpdk-dev] [PATCH RFC v2 12/12] lib/librte_vhost: cleanup when vhost user socket connection is closed Huawei Xie 2014-12-10 22:04 ` [dpdk-dev] [PATCH RFC v2 00/12] lib/librte_vhost: vhost-user support Xie, Huawei 2014-12-11 2:21 ` Tetsuya Mukawa 2014-12-15 5:26 ` Tetsuya Mukawa 2014-12-17 17:43 ` Xie, Huawei 2015-01-07 12:43 ` Qiu, Michael 2015-01-23 8:16 ` Linhaifeng 2015-01-26 7:24 ` Xie, Huawei
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=1418247477-13920-8-git-send-email-huawei.xie@intel.com \ --to=huawei.xie@intel.com \ --cc=dev@dpdk.org \ --cc=haifeng.lin@intel.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
DPDK patches and discussions This inbox may be cloned and mirrored by anyone: git clone --mirror https://inbox.dpdk.org/dev/0 dev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 dev dev/ https://inbox.dpdk.org/dev \ dev@dpdk.org public-inbox-index dev Example config snippet for mirrors. Newsgroup available over NNTP: nntp://inbox.dpdk.org/inbox.dpdk.dev AGPL code for this site: git clone https://public-inbox.org/public-inbox.git