diff options
Diffstat (limited to 'src/nSocket/nstack/event')
-rw-r--r-- | src/nSocket/nstack/event/epoll/nstack_epoll.c | 887 | ||||
-rw-r--r-- | src/nSocket/nstack/event/epoll/nstack_eventpoll.c | 1215 | ||||
-rw-r--r-- | src/nSocket/nstack/event/select/nstack_select.c | 1368 | ||||
-rw-r--r-- | src/nSocket/nstack/event/select/select_adapt.c | 301 |
4 files changed, 1911 insertions, 1860 deletions
diff --git a/src/nSocket/nstack/event/epoll/nstack_epoll.c b/src/nSocket/nstack/event/epoll/nstack_epoll.c new file mode 100644 index 0000000..d1a006f --- /dev/null +++ b/src/nSocket/nstack/event/epoll/nstack_epoll.c @@ -0,0 +1,887 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "nstack_eventpoll.h" +#include "nstack_log.h" +#include "nsfw_recycle_api.h" +#include "nstack_securec.h" +#include "nstack_module.h" +#include "nstack_sockops.h" +#include "nsfw_mem_api.h" +#include "nstack_fd_mng.h" +#include "nstack.h" +#include "dmm_spinlock.h" +#include "nsfw_base_linux_api.h" +#include "nstack_dmm_dfx.h" +#include "nstack_epoll_api.h" +#ifdef __cplusplus +/* *INDENT-OFF* */ +extern "C"{ +/* *INDENT-ON* */ +#endif /* __cplusplus */ + +const int EPOLL_DFX_OPS_MAP[nstack_ep_event_max] = + { DMM_APP_EPOLL_ADD_TICK, DMM_APP_EPOLL_MOD_TICK, DMM_APP_EPOLL_DEL_TICK, + DMM_DFX_MAX +}; + +static inline void *nstack_ep_triggle + (int proFD, + int modInx, + int triggle_ops, struct epitem *epi, nsep_epollInfo_t * epinfo) +{ + void *data = NULL; + int events = 0; + + if (nstack_fd_deal[modInx].ep_triggle != NULL) + { + data = + nstack_fd_deal[modInx].ep_triggle(proFD, triggle_ops, epinfo, + &events); + if (data && epi) + { + struct stat_epfd_info tmp_epfd_info; + struct eventpoll *ep = + (struct eventpoll *) SHMEM_ADDR_SHTOL(epi->ep); + switch (triggle_ops) + { + case nstack_ep_triggle_add: + if (epi->event.events & events) + { + NSTACK_EPOLL_EVENT_ADD(epinfo, events, + EVENT_INFORM_APP); + } + break; + case nstack_ep_triggle_mod: + if (epi->event.events & events) + { + NSTACK_EPOLL_EVENT_ADD(epinfo, events, + EVENT_INFORM_APP); + } + else + { + NSTACK_EPOLL_EVENT_DEL(epi, EPOLLET); + } + break; + default: + break; + } + tmp_epfd_info.epoll_wait_tick = ep->epoll_wait_tick; + tmp_epfd_info.epfd = ep->epfd; + tmp_epfd_info.epoll_fork_flag = ep->epoll_fork_flag; + tmp_epfd_info.hhpid = get_sys_pid(); + nsep_epollInfo_t *epfd_info = nsep_get_info_by_sock(ep->epfd); + if (NULL != epfd_info) + tmp_epfd_info.ep_sleepTime = epfd_info->sleepTime; + else + tmp_epfd_info.ep_sleepTime = 0; + nstack_dfx_state_update((u64) proFD, modInx, + EPOLL_DFX_OPS_MAP[triggle_ops], + &tmp_epfd_info); + } + } + return data; +} + +#define nstack_ep_get_evt(_epInfo, _epi) do\ +{\ + if ((_epInfo)->rmidx != -1 && nstack_fd_deal[(_epInfo)->rmidx].ep_getEvt != NULL)\ + {\ + int evt_events;\ + evt_events= nstack_fd_deal[(_epInfo)->rmidx].ep_getEvt((_epInfo)->rlfd);\ + if(((_epi)->event.events & EPOLLIN) && (evt_events & EPOLLIN))\ + (_epi)->revents |=EPOLLIN;\ + if(((_epi)->event.events & EPOLLOUT) && (evt_events & EPOLLOUT))\ + (_epi)->revents |=EPOLLOUT; \ + }\ +}while(0)\ + +#define NSEP_IS_SOCK_VALID(_sock) ((_sock) >= 0 && (u32_t)(_sock) < NSTACK_KERNEL_FD_MAX) + +/* + * Triggle epoll events of stack + * ep - eventpoll instance + * fdInf - file descriptor of stack + * triggle_ops - why triggle + */ +/*no need null pointer check*/ +void nsep_epctl_triggle(struct epitem *epi, nsep_epollInfo_t * info, + int triggle_ops) +{ + int modInx; + int protoFd = -1; + void *data = NULL; + + NSSOC_LOGDBG("info=%p,info->rmidx=%d,triggle_ops=%d", info, info->rmidx, + triggle_ops); + + /* Now need to triggle userspace network stack events after add operation */ + if (info->rmidx >= 0) + { + if (info->rmidx != nstack_get_linux_mid()) + { + /* fix overflow type */ + if ((info->rmidx >= NSEP_SMOD_MAX) + || (info->rmidx >= NSTACK_MAX_MODULE_NUM)) + { + return; + } + data = + nstack_ep_triggle(info->rlfd, info->rmidx, triggle_ops, epi, + info); + if ((NULL != data) && (nstack_ep_triggle_add == triggle_ops)) + { + info->private_data = (void *) SHMEM_ADDR_LTOSH(data); + info->epaddflag[info->rmidx] = 1; + } + NSSOC_LOGDBG + ("info=%p,module=%s,protoFd=%d,triggle_ops=%d, ret:%p", info, + nstack_get_module_name_by_idx(info->rmidx), info->rlfd, + triggle_ops, data); + } + } + else + { + nstack_each_mod_inx(modInx) + { + if ((modInx >= NSEP_SMOD_MAX) + || (modInx >= NSTACK_MAX_MODULE_NUM)) + { + return; + } + protoFd = info->protoFD[modInx]; + if (modInx == nstack_get_linux_mid() || -1 == protoFd) + continue; // Don't do anything , epoll_wait will do for you + + data = nstack_ep_triggle(protoFd, modInx, triggle_ops, epi, info); + if ((NULL != data) && (nstack_ep_triggle_add == triggle_ops)) + { + info->private_data = (void *) SHMEM_ADDR_LTOSH(data); + info->epaddflag[modInx] = 1; + } + NSSOC_LOGDBG + ("info=%p,module=%s,protoFd=%d,triggle_ops=%d, ret:%p", info, + nstack_get_module_name_by_idx(modInx), protoFd, triggle_ops, + data); + } + } +} + +NSTACK_STATIC + void nsep_rbtree_insert(struct eventpoll *ep, struct epitem *epi) +{ + + struct ep_rb_node **p = &ep->rbr.rb_node, *parent = NULL; /*not null here */ + struct epitem *epic; + u32_t loopCnt = 0; + + while (*p) + { + ++loopCnt; + if (loopCnt > NSTACK_MAX_EPITEM_NUM) + { + NSSOC_LOGERR("Loop out of range!!!!"); + break; + } + + parent = (struct ep_rb_node *) SHMEM_ADDR_SHTOL(*p); + + epic = ep_rb_entry(parent, struct epitem, rbn); + + if (epi->fd > epic->fd) + { + p = &(parent->rb_right); + } + else + { + p = &(parent->rb_left); + } + } + + ep_rb_link_node(&epi->rbn, parent, p); + ep_rb_insert_color(&epi->rbn, &ep->rbr); /*not null here */ + +} + +void _InOrder(struct ep_rb_node *root) +{ + struct epitem *epi = NULL; + nsep_epollInfo_t *epInfo = NULL; + stat_epitem_info_t epitem_info; + + if (NULL == root) + { + return; + } + + _InOrder((struct ep_rb_node *) SHMEM_ADDR_SHTOL(root->rb_left)); + + epi = ep_rb_entry(root, struct epitem, rbn); + + epInfo = (nsep_epollInfo_t *) SHMEM_ADDR_SHTOL(epi->private_data); + if (NULL != epInfo) + { + epitem_info.event = epi->event; + epitem_info.is_linked = EP_HLIST_NODE_LINKED(&epi->rdllink); + nstack_dfx_state_update((u64) epInfo->rlfd, epInfo->rmidx, + DMM_APP_EPOLL_WAIT_FAIL, &epitem_info); + } + _InOrder((struct ep_rb_node *) SHMEM_ADDR_SHTOL(root->rb_right)); + +} + +void nsep_notify_fd_epoll_wait_fail(struct eventpoll *ep) +{ + + if (!ep) + return; + + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&ep->sem)); /*do not need return value */ + _InOrder((struct ep_rb_node *) SHMEM_ADDR_SHTOL(ep->rbr.rb_node)); + dmm_spin_unlock((dmm_spinlock_t *) (&ep->sem)); +} + +/* + * This function is called by epctl_add , it will create one epitem of fd, and insert to eventpoll + */ +NSTACK_STATIC + int nsep_insert_node(struct eventpoll *ep, + nsep_epollInfo_t * epfd_epInfo, + struct epoll_event *event, nsep_epollInfo_t * epInfo) +{ + struct epitem *epi; + + if (nsep_alloc_epitem(&epi)) + { + NSSOC_LOGERR("Can't alloc epitem"); + errno = ENOMEM; + return -1; + } + + EP_HLIST_INIT_NODE(&epi->rdllink); + + epi->ptr_reserve = NULL; + epi->ep_spl = (struct spl_conn_pool *) (epfd_epInfo->ep_conn); + epi->ep = (struct eventpoll *) SHMEM_ADDR_LTOSH_EXT(ep); + epi->epInfo = (nsep_epollInfo_t *) SHMEM_ADDR_LTOSH_EXT(epInfo); + epi->revents = 0; + epi->event = *event; /*no need null pointer check */ + + EP_LIST_INIT_NODE(&epi->fllink); + EP_HLIST_INIT_NODE(&epi->txlink); + epi->fd = epInfo->fd; /*no need null pointer check */ + + /* Add the current item to the list of active epoll hook for this file + This should lock because file descriptor may be called by other eventpoll */ + + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&epInfo->epiLock)); /*do not need return value */ + ep_list_add_tail(&epInfo->epiList, &epi->fllink); + epi->private_data = (void *) SHMEM_ADDR_LTOSH_EXT(epInfo); + dmm_spin_unlock((dmm_spinlock_t *) (&epInfo->epiLock)); + + /* Add epitem to eventpoll fd list, don't need lock here because epoll_ctl will lock before calling this function */ + nsep_rbtree_insert(ep, epi); + + /* Need to poll the events already stored in stack */ + nsep_epctl_triggle(epi, epInfo, nstack_ep_triggle_add); + + NSSOC_LOGINF("epfd=%d,ep=%p,fd=%d,epi=%p", ep->epfd, ep, epInfo->fd, epi); + + return 0; +} + +#ifdef KERNEL_FD_SUPPORT +NSTACK_STATIC int nsep_is_add_valid(int fd, struct epoll_event *events) +{ + if (-1 == nsep_get_manager()->checkEpollFD) + { + return -1; + } + if (-1 == + nsfw_base_epoll_ctl(nsep_get_manager()->checkEpollFD, EPOLL_CTL_ADD, + fd, events)) + { + return -1; + } + + nsfw_base_epoll_ctl(nsep_get_manager()->checkEpollFD, EPOLL_CTL_DEL, fd, NULL); /*do not need return value */ + return 0; +} + +NSTACK_STATIC + int nsep_add_to_kernel(int epfd, int fd, const struct epoll_event *events) +{ + struct epoll_event tmpEvt; + tmpEvt.data.fd = fd; + tmpEvt.events = events->events; /*no need null pointer check */ + NSSOC_LOGINF("epfd=%d,fd=%d,events=%u", epfd, fd, events->events); + return nsfw_base_epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &tmpEvt); +} + +NSTACK_STATIC + int nsep_mod_to_kernel(int epfd, int fd, const struct epoll_event *events) +{ + struct epoll_event tmpEvt; + tmpEvt.data.fd = fd; + tmpEvt.events = events->events; /*no need null pointer check */ + NSSOC_LOGINF("epfd=%d,fd=%d,events=%u", epfd, fd, events->events); + return nsfw_base_epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &tmpEvt); +} +#endif + +int nsep_epctl_add(struct eventpoll *ep, nsep_epollInfo_t * epfd_epInfo, + int fd, struct epoll_event *events) +{ + + int ret = 0; + + NSSOC_LOGINF("epfd=%d,fd=%d,events=%u", ep->epfd, fd, events->events); + + nsep_epollInfo_t *epInfo = nsep_get_info_by_sock(fd); + + if (NULL == epInfo) + { +#ifdef KERNEL_FD_SUPPORT + if (-1 == nsep_is_add_valid(fd, events)) + { + NSSOC_LOGERR("Invalid add check nfd=%d]", fd); + return -1; + } + if (-1 == nsep_alloc_info_with_sock(fd)) + { + NSSOC_LOGERR("Can't alloc epInfo for nfd]nfd=%d", fd); + return -1; + } + nsep_set_info_proto_fd(fd, nstack_get_linux_mid(), fd); + epInfo = nsep_get_info_by_sock(fd); +#else + /*if FD is not in use, epoll_ctl_add return fail */ + NSSOC_LOGERR("Invalid add check nfd]nfd=%d", fd); + return -1; +#endif + } + + if (NULL == epfd_epInfo) + { + NSSOC_LOGWAR("epfd_epInfo is NULL]epfd=%d,fd=%d ", ep->epfd, + epInfo->fd); + return -1; + } + + ret = nsep_insert_node(ep, epfd_epInfo, events, epInfo); + if (0 != ret) + { + NSSOC_LOGWAR("insert fail]epfd=%d,fd=%d ", ep->epfd, epInfo->fd); + return -1; + } + +#ifdef KERNEL_FD_SUPPORT + /* Add fd to epoll fd to support kernel epoll thread */ + if (-1 == epInfo->rmidx || epInfo->rmidx == nstack_get_linux_mid()) + { + if (-1 == nsep_add_to_kernel(ep->epfd, epInfo->fd, events)) + { /*no need null pointer check */ + NSSOC_LOGWAR("epctl fail]epfd=%d,fd=%d,errno=%d", ep->epfd, + epInfo->fd, errno); + return -1; + } + } +#endif + + return 0; +} + +int nsep_epctl_del(struct eventpoll *ep, struct epitem *epi) +{ + int ret = 0; + + nsep_epollInfo_t *epInfo = (nsep_epollInfo_t *) SHMEM_ADDR_SHTOL(epi->private_data); /*no need null pointer check */ + NSSOC_LOGINF("epfd=%d,fd=%d,epi=%p", ep->epfd, epi->fd, epi); + +#ifdef KERNEL_FD_SUPPORT + nsfw_base_epoll_ctl(ep->epfd, EPOLL_CTL_DEL, epi->fd, NULL); /*do not need return value */ +#endif + nsep_epctl_triggle(epi, epInfo, nstack_ep_triggle_del); + + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&epInfo->epiLock)); /*do not need return value */ + /* Here need to check because nstack_close may has removed this epi->fllink */ + ep_list_del(&epInfo->epiList, &epi->fllink); /*no need null pointer check */ + /* after close fd in epfd, epfd still can epoll_wait EPOLLIN event for these fd */ + (void) nstack_epoll_event_dequeue(epi, 0); + dmm_spin_unlock((dmm_spinlock_t *) (&epInfo->epiLock)); + nsep_epctl_triggle(NULL, epInfo, nstack_ep_triggle_inform_app); + + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&ep->lock)); /*do not need return value */ + ret = nstack_ep_unlink(ep, epi); + dmm_spin_unlock((dmm_spinlock_t *) (&ep->lock)); /*no need null pointer check */ + nsep_free_epitem(epi); /*do not need return value */ + + return ret; +} + +/*no need null pointer check*/ +int nsep_epctl_mod(struct eventpoll *ep, + nsep_epollInfo_t * epInfo, + struct epitem *epi, struct epoll_event *events) +{ + if (NULL == epInfo) + { + errno = EINVAL; + NSSOC_LOGWAR("epfd=%d, intput epInfo is null err", ep->epfd); + return -1; + } + + NSSOC_LOGINF("epfd=%d,fd=%d,events=%u", ep->epfd, epInfo->fd, + events->events); + + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&ep->lock)); /*do not need return value */ + epi->event = *events; /* kernel tells me that I need to modify epi->event in lock context */ + dmm_spin_unlock((dmm_spinlock_t *) (&ep->lock)); + +#ifdef KERNEL_FD_SUPPORT + /* Modify fd to epoll fd to support kernel epoll thread */ + if (-1 == epInfo->rmidx || epInfo->rmidx == nstack_get_linux_mid()) + { + if (-1 == nsep_mod_to_kernel(ep->epfd, epInfo->fd, events)) + { + NSSOC_LOGWAR("epctl fail]epfd=%d,fd=%d,errno=%d", ep->epfd, + epInfo->fd, errno); + return -1; + } + } +#endif + + nsep_epctl_triggle(epi, epInfo, nstack_ep_triggle_mod); + return 0; +} + +/* + * Called by epoll_wait + * Wait until events come or timeout + */ + /*no need to check return value */ +int nsep_ep_poll(struct eventpoll *ep, struct epoll_event *events, + int maxevents, struct spl_conn_pool *ep_conn) +{ + int evt = 0; + struct ep_hlist_node *node = NULL; + struct epitem *epi = NULL; + i32 enQueRet = 0; + void *ring_hd = NULL; + unsigned int tmp_revents = 0; + + if (maxevents <= 0 || !events) + return 0; + + if (NULL == ep_conn) + { + goto rdlist_check; + } + + ring_hd = SHMEM_ADDR_SHTOL(ep_conn->ring_hd); + + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&ep->sem)); + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&ep->lock)); + while (1) + { + enQueRet = nsfw_mem_ring_dequeue(ring_hd, (void **) &epi); + if (1 != enQueRet) + { + break; + } + /* dont clear epi successfully, it cause app coredump */ + nsep_epollInfo_t *epInfo = + (nsep_epollInfo_t *) SHMEM_ADDR_SHTOL(epi->private_data); + if ((NULL == epInfo) || (NULL == epi->ep)) + { + NSPOL_LOGERR + ("epInfo or ep is NULL]ep_conn=%p,ep=%p,ring_hd=%p,epi=%p,epi->ep=%p,epInfo=%p", + ep_conn, ep, ring_hd, epi, SHMEM_ADDR_SHTOL(epi->ep), + epInfo); + continue; + } + epi->app_poll_count = epi->spl_enter_count; + /* dont clear epi successfully, it cause app coredump */ + if (ep != SHMEM_ADDR_SHTOL(epi->ep)) + { + NSPOL_LOGERR + ("ep_conn use by multi ep]ep_conn=%p,ep=%p,ring_hd=%p,epi=%p,epInfo=%p,epInfo->fd=%d,epi->ep=%p", + ep_conn, ep, ring_hd, epi, epInfo, epInfo->fd, + SHMEM_ADDR_SHTOL(epi->ep)); + continue; + } + + nstack_dfx_state_update((u64) epInfo->rlfd, epInfo->rmidx, + DMM_APP_EPOLL_WAIT_GET_TICK, NULL); + + if (!EP_HLIST_NODE_LINKED(&epi->rdllink)) + { + ep_hlist_add_tail(&ep->rdlist, &epi->rdllink); + } + + }; + + dmm_spin_unlock((dmm_spinlock_t *) (&ep->lock)); + dmm_spin_unlock((dmm_spinlock_t *) (&ep->sem)); + + rdlist_check: + + if (EP_HLIST_EMPTY(&ep->rdlist)) + { + NSSOC_LOGDBG("ep->rdlist is Empty, epfd=%d", ep->epfd); + return 0; + } + + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&ep->sem)); /*do not need return value */ + if (EP_HLIST_EMPTY(&ep->rdlist)) + goto out; + + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&ep->lock)); /*do not need return value */ + + struct ep_hlist_node *epRdHead = + (struct ep_hlist_node *) SHMEM_ADDR_SHTOL(ep->rdlist.head); + if (!epRdHead) + { + return 0; + } + node = (struct ep_hlist_node *) SHMEM_ADDR_SHTOL(epRdHead->next); + + while (node) + { + + epi = ep_hlist_entry(node, struct epitem, rdllink); + + node = (struct ep_hlist_node *) SHMEM_ADDR_SHTOL(node->next); + nsep_epollInfo_t *epInfo = + (nsep_epollInfo_t *) SHMEM_ADDR_SHTOL(epi->private_data); + epInfo = (nsep_epollInfo_t *) SHMEM_ADDR_SHTOL(epi->private_data); + + nstack_ep_get_evt(epInfo, epi); + + tmp_revents = epi->revents; + + /* app epoll_wait return 24 event, when the fd is in establish state */ + + while (!__sync_bool_compare_and_swap(&epi->revents, tmp_revents, 0)) + { /* daemon-stack don't have lock for err hup rdhup event, so here must ensure that daemon-stack don't modify it */ + tmp_revents = epi->revents; + }; + + if (tmp_revents) + { + events[evt].events = tmp_revents; + events[evt].data = epi->event.data; + NSSOC_LOGDBG("Add event]epfd=%d,fd=%d,events=%u", ep->epfd, + epi->fd, events[evt].events); + evt++; + nstack_dfx_state_update((u64) epInfo->rlfd, epInfo->rmidx, + DMM_APP_EPOLL_WAIT_EVENT, + (void *) (u64) tmp_revents); + } + + if (0 == tmp_revents || epi->event.events & EPOLLET) + { + NSSOC_LOGDBG("Del epi->rdllink,epfd=%d,fd=%d", ep->epfd, epi->fd); + ep_hlist_del(&ep->rdlist, &epi->rdllink); + } + + if (tmp_revents & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) + { + NSSOC_LOGINF("epfd=%d,fd=%d,epi=%p,revent=%u", ep->epfd, epi->fd, + epi, tmp_revents); + } + + if (evt >= maxevents) + break; + } + dmm_spin_unlock((dmm_spinlock_t *) (&ep->lock)); + out: + dmm_spin_unlock((dmm_spinlock_t *) (&ep->sem)); + + NSSOC_LOGDBG("Return epfd=%d,fd=%d,EP_HLIST_EMPTY(&ep->rdlist)=%d", + ep->epfd, epi ? epi->fd : -1, EP_HLIST_EMPTY(&ep->rdlist)); + + return evt; +} + +/*no need to check return value*/ + +void nsep_remove_epfd(nsep_epollInfo_t * pinfo) +{ + pid_t pid = get_sys_pid(); + struct list_node *prenode = NULL; + struct list_node *nextnode = NULL; + struct list_node **node_arry = NULL; + int lenth = NSTACK_MAX_EPOLL_INFO_NUM * sizeof(struct list_node *); + struct epitem *epi = NULL; + struct epitem *tepi = NULL; + struct eventpoll *ep = NULL; + u32_t i = 0; + u32_t icnt = 0; + + if (!pinfo) + { + return; + } + /*malloc a block memory to store epitem node, do not use list for maybe free item */ + /*malloc() & free() can be used */ + node_arry = (struct list_node **) malloc(lenth); + if (!node_arry) + { + NSSOC_LOGERR("remove fd from ep malloc mem fail]fd=%d,ep=%p", + pinfo->fd, pinfo->ep); + return; + } + /*add return value check */ + int retVal = memset_s(node_arry, lenth, 0, lenth); + if (EOK != retVal) + { + NSSOC_LOGERR("memset_s failed]retVal=%d", retVal); + free(node_arry); /*free() can be used */ + return; + } + + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&pinfo->epiLock)); /*do not need return value */ + /*list head must be not null */ + prenode = (struct list_node *) SHMEM_ADDR_SHTOL(pinfo->epiList.head); + nextnode = (struct list_node *) SHMEM_ADDR_SHTOL(prenode->next); + icnt = 0; + + /*find all node that pid is belong to itself */ + while (nextnode) + { + if (++i > NSTACK_MAX_EPOLL_INFO_NUM) + { + /*record the exception log */ + NSSOC_LOGERR("error maybe happen]free pinfo=%p", pinfo); + break; + } + + epi = ep_list_entry(nextnode, struct epitem, fllink); + if (pid == epi->pid) + { + prenode->next = nextnode->next; + nextnode->next = NULL; + /*put into release list */ + node_arry[icnt] = nextnode; + icnt++; + /* only can clear ring for epi that del */ + (void) nstack_epoll_event_dequeue(epi, 0); + } + else + { + prenode = nextnode; + } + nextnode = (struct list_node *) SHMEM_ADDR_SHTOL(prenode->next); + } + + dmm_spin_unlock((dmm_spinlock_t *) (&pinfo->epiLock)); + + /*free all epitem */ + for (i = 0; i < icnt; i++) + { + + epi = ep_list_entry(node_arry[i], struct epitem, fllink); + ep = (struct eventpoll *) SHMEM_ADDR_SHTOL(epi->ep); + if (ep) + { + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&ep->sem)); + /* Here don't use epi you find before, use fd and ep to find the epi again.that is multithread safe */ + tepi = nsep_find_ep(ep, pinfo->fd); + /*record the exception log */ + if (epi != tepi) + { + NSSOC_LOGERR("remove fd:%d epi:%p tepi:%p erro maybe happen", + pinfo->fd, epi, tepi); + } + /*if tepi is null, epi maybe free by nsep_close_epfd, so no need to free again */ + if (tepi) + { +#ifdef KERNEL_FD_SUPPORT + nsfw_base_epoll_ctl(ep->epfd, EPOLL_CTL_DEL, tepi->fd, NULL); /*do not need return value */ +#endif + nsep_epctl_triggle(tepi, pinfo, nstack_ep_triggle_del); + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&ep->lock)); /*do not need return value */ + (void) nstack_ep_unlink(ep, tepi); + dmm_spin_unlock((dmm_spinlock_t *) (&ep->lock)); + + nsep_free_epitem(epi); + } + dmm_spin_unlock((dmm_spinlock_t *) (&ep->sem)); + } + } + + nsep_epctl_triggle(NULL, pinfo, nstack_ep_triggle_inform_app); + /*malloc() & free() can be used */ + free(node_arry); + return; +} + +void nsep_close_epfd(struct eventpoll *ep) +{ + + if (!ep) + return; + + struct epitem *epi = NULL; + struct ep_rb_node *node = NULL; + + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&ep->sem)); /*do not need return value */ + while ((node = ep_rb_first(&ep->rbr))) + { + + epi = ep_rb_entry(node, struct epitem, rbn); + + int ret = nsep_epctl_del(ep, epi); + + /* Avoid dead loop */ + if (ret) + { + NSSOC_LOGERR + ("nstack epctl del fail, will break to avoid dead loop]ep->fd=%d,epi->fd=%d", + ep->epfd, epi->fd); + break; + } + } + dmm_spin_unlock((dmm_spinlock_t *) (&ep->sem)); + nsep_free_eventpoll(ep); /*do not need return value */ +} + +/*no need to check null pointer*/ +static inline int nsp_epoll_close_kernel_fd(int sock, + nsep_epollInfo_t * epInfo) +{ + NSSOC_LOGINF("fd=%d,type=%d", sock, epInfo->fdtype); + int ret = 0; + nsep_remove_epfd(epInfo); + + u32_t pid = get_sys_pid(); + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&epInfo->freeLock)); /*do not need return value */ + int left_count = nsep_del_last_pid(&epInfo->pidinfo, pid); + dmm_spin_unlock((dmm_spinlock_t *) (&epInfo->freeLock)); + if (-1 == left_count) + { + NSSOC_LOGERR("pid not exist]fd=%d,type=%d,pid=%u", sock, + epInfo->fdtype, pid); + } + + if (0 == left_count) + { + ret = nsep_free_epinfo(epInfo); + NSSOC_LOGINF("epinfo removed]fd=%d,type=%d", sock, epInfo->fdtype); + } + + return ret; +} + +/*no need to check null pointer*/ +static inline int nsp_epoll_close_spl_fd(int sock, nsep_epollInfo_t * epInfo) +{ + NSSOC_LOGINF("fd=%d,type=%d", sock, epInfo->fdtype); + nsep_remove_epfd(epInfo); /*no need to check null pointer */ + return 0; +} + +/*no need to check null pointer*/ +static inline int nsp_epoll_close_ep_fd(int sock, nsep_epollInfo_t * epInfo) +{ + struct eventpoll *ep = SHMEM_ADDR_SHTOL(epInfo->ep); + struct spl_conn_pool *ep_conn = SHMEM_ADDR_SHTOL(epInfo->ep_conn); + u32_t pid = get_sys_pid(); + NSSOC_LOGINF("fd:%d is epoll fd ep:%p]", sock, ep); + dmm_spin_lock_with_pid((dmm_spinlock_t *) (&epInfo->freeLock)); + int left_count = nsep_del_last_pid(&epInfo->pidinfo, pid); + dmm_spin_unlock((dmm_spinlock_t *) (&epInfo->freeLock)); + if (0 == left_count) + { + epInfo->ep = NULL; + epInfo->ep_conn = NULL; + nsep_close_epfd(ep); + /* must close ep fistly before free ep_conn, becasue if close ep_conn firstly, then this ep_conn will alloc by other epfd, + this time, now daemon-stack possiblely will report event using this ep_conn */ + (void) nsep_free_ep_spl_conn_ring(ep_conn); + nsep_free_epinfo(epInfo); + } + return 0; +} + +/*no need to check null pointer*/ +int nsep_epoll_close(int sock) +{ + int ret = 0; + nsep_epollInfo_t *epInfo = nsep_get_info_by_sock(sock); + if (!epInfo) + { + NSSOC_LOGDBG("epollsock close sock:%d is not exist", sock); + return 0; + } + + nsep_set_info_sock_map(sock, NULL); + + if (NSTACK_EPOL_FD == epInfo->fdtype) + { + return nsp_epoll_close_ep_fd(sock, epInfo); + } + + if (epInfo->rmidx == nstack_get_linux_mid()) + { + ret = nsp_epoll_close_kernel_fd(sock, epInfo); + } + else + { + ret = nsp_epoll_close_spl_fd(sock, epInfo); + } + + return ret; +} + +/* epinfo add pid */ +void nsep_fork(u32_t ppid) +{ + u32 cpid = get_sys_pid(); + nsep_epollManager_t *manager = nsep_get_manager(); + if (NULL == manager->infoSockMap) + { + NSSOC_LOGERR("infoSockMap is NULL]ppid=%u,cpid=%u", ppid, cpid); + return; + } + + nsep_epollInfo_t *epinfo = NULL; + int pos; + for (pos = 0; (u32_t) pos < NSTACK_KERNEL_FD_MAX; pos++) + { + epinfo = manager->infoSockMap[pos]; + if (epinfo) + { + if (nsep_add_pid(&epinfo->pidinfo, cpid) != 0) + { + NSSOC_LOGERR("epinfo add pid failed]fd=%d,ppid=%u.cpid=%u", + pos, ppid, cpid); + } + else + { + NSSOC_LOGDBG("epinfo add pid ok]fd=%d,ppid=%u.cpid=%u", pos, + ppid, cpid); + } + } + } +} + +#ifdef __cplusplus +/* *INDENT-OFF* */ +} +/* *INDENT-ON* */ +#endif /* __cplusplus */ diff --git a/src/nSocket/nstack/event/epoll/nstack_eventpoll.c b/src/nSocket/nstack/event/epoll/nstack_eventpoll.c deleted file mode 100644 index a8b3757..0000000 --- a/src/nSocket/nstack/event/epoll/nstack_eventpoll.c +++ /dev/null @@ -1,1215 +0,0 @@ -/* -* -* Copyright (c) 2018 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at: -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include "nstack_eventpoll.h" -#include "nstack_log.h" -#include "nsfw_recycle_api.h" -#include "nstack_securec.h" -#include "nstack_module.h" -#include "nstack_sockops.h" -#include "nsfw_mem_api.h" -#include "nstack_fd_mng.h" -#include "nstack.h" -#include "nstack_dmm_adpt.h" - -#ifdef __cplusplus -/* *INDENT-OFF* */ -extern "C"{ -/* *INDENT-ON* */ -#endif /* __cplusplus */ - -#define NSTACK_EP_TRIGGLE(epFD, proFD, modInx, triggle_ops, event, pdata, ret) \ - if (nstack_extern_deal(modInx).ep_ctl != NULL) \ - { \ - ret = nstack_extern_deal(modInx).ep_ctl((epFD), (proFD), (triggle_ops), (event), pdata); \ - } - -#define nstack_ep_getEvt(epFD, profd, modInx, events, ret) \ - if (nstack_extern_deal(modInx).ep_getevt != NULL)\ - { \ - ret |= nstack_extern_deal(modInx).ep_getevt((epFD), (profd), (events)); \ - } - -#define NSEP_IS_SOCK_VALID(_sock) ((_sock) >= 0 && (u32_t)(_sock) < NSTACK_KERNEL_FD_MAX) - -/*add event to rdlist if event have come in*/ -void -nsep_ctl_event_add (struct epitem *epi, struct eventpoll *ep, - int triggle_ops, unsigned int events) -{ - switch (triggle_ops) - { - case nstack_ep_triggle_add: - if (events & epi->event.events) - { - sys_arch_lock_with_pid (&ep->lock); - - if (!EP_HLIST_NODE_LINKED (&epi->rdllink)) - { - ep_hlist_add_tail (&ep->rdlist, &epi->rdllink); - } - - sys_sem_s_signal (&ep->lock); - sem_post (&ep->waitSem); - } - - break; - - case nstack_ep_triggle_mod: - sys_arch_lock_with_pid (&ep->lock); - - if (events & epi->event.events) - { - if (!EP_HLIST_NODE_LINKED (&epi->rdllink)) - { - ep_hlist_add_tail (&ep->rdlist, &epi->rdllink); - sem_post (&ep->waitSem); - } - } - else - { - if (EP_HLIST_NODE_LINKED (&epi->rdllink) - && (epi->event.events & EPOLLET)) - { - ep_hlist_del (&ep->rdlist, &epi->rdllink); - } - } - - sys_sem_s_signal (&ep->lock); - break; - default: - break; - } - - return; -} - -/* - * Triggle epoll events of stack - * ep - eventpoll instance - * fdInf - file descriptor of stack - * triggle_ops - why triggle - */ -NSTACK_STATIC void -nsep_epctl_triggle (struct epitem *epi, nsep_epollInfo_t * info, - int triggle_ops) -{ - int modInx; - int protoFd = -1; - int epfd = 0; - int ret = 0; - struct eventpoll *ep = NULL; - nsep_epollInfo_t *epInfo = NULL; - - ep = ADDR_SHTOL (epi->ep); - epfd = ep->epfd; - epInfo = nsep_get_infoBySock (epfd); - - NSSOC_LOGINF ("info=%p,info->rmidx=%d,triggle_ops=%d", info, info->rmidx, - triggle_ops); - - /* Now need to triggle userspace network stack events after add operation */ - if (info->rmidx >= 0) - { - /* fix overflow type codex issue */ - if ((info->rmidx >= NSEP_SMOD_MAX) - || (info->rmidx >= NSTACK_MAX_MODULE_NUM)) - { - return; - } - - NSTACK_EP_TRIGGLE (epInfo->protoFD[info->rmidx], info->rlfd, - info->rmidx, triggle_ops, &(epi->event), info, ret); - - if ((ret >= 0) - && ((nstack_ep_triggle_add == triggle_ops) - || (nstack_ep_triggle_mod == triggle_ops))) - { - epi->revents = ret; - nsep_ctl_event_add (epi, ep, triggle_ops, epi->revents); - info->epaddflag |= (1 << info->rmidx); - } - NSSOC_LOGINF ("info=%p,module=%s,protoFd=%d,triggle_ops=%d, ret:%d", - info, nstack_get_module_name_by_idx (info->rmidx), - info->rlfd, triggle_ops, ret); - } - else - { - nstack_each_modInx (modInx) - { - protoFd = info->protoFD[modInx]; - - if (protoFd < 0) - { - continue; - } - NSTACK_EP_TRIGGLE (epInfo->protoFD[modInx], protoFd, modInx, - triggle_ops, &(epi->event), info, ret); - if ((ret >= 0) - && ((nstack_ep_triggle_add == triggle_ops) - || (nstack_ep_triggle_mod == triggle_ops))) - { - epi->revents |= ret; - nsep_ctl_event_add (epi, ep, triggle_ops, epi->revents); - info->epaddflag |= (1 << modInx); - } - NSSOC_LOGINF ("info=%p,module=%s,protoFd=%d,triggle_ops=%d, ret:%d", - info, nstack_get_module_name_by_idx (modInx), protoFd, - triggle_ops, ret); - } - } - return; -} - -NSTACK_STATIC void -nsep_rbtree_insert (struct eventpoll *ep, struct epitem *epi) -{ - struct ep_rb_node **p = &ep->rbr.rb_node, *parent = NULL; - struct epitem *epic; - u32_t loopCnt = 0; - - while (*p) - { - ++loopCnt; - if (loopCnt > NSTACK_MAX_EPITEM_NUM) - { - NSSOC_LOGERR ("Loop out of range!!!!"); - break; - } - - parent = (struct ep_rb_node *) ADDR_SHTOL (*p); - epic = ep_rb_entry (parent, struct epitem, rbn); - if (epi->fd > epic->fd) - { - p = &(parent->rb_right); - } - else - { - p = &(parent->rb_left); - } - } - - ep_rb_link_node (&epi->rbn, parent, p); - ep_rb_insert_color (&epi->rbn, &ep->rbr); -} - -/* - * This function is called by epctl_add , it will create one epitem of fd, and insert to eventpoll - */ -NSTACK_STATIC int -nsep_insert_node (struct eventpoll *ep, struct epoll_event *event, - nsep_epollInfo_t * fdInfo) -{ - struct epitem *epi; - - if (nsep_alloc_epitem (&epi)) - { - NSSOC_LOGERR ("Can't alloc epitem"); - errno = ENOMEM; - return -1; - } - - EP_HLIST_INIT_NODE (&epi->rdllink); - EP_HLIST_INIT_NODE (&epi->lkFDllink); - - epi->nwait = 0; - epi->ep = (struct eventpoll *) ADDR_LTOSH_EXT (ep); - epi->epInfo = (nsep_epollInfo_t *) ADDR_LTOSH_EXT (fdInfo); - epi->revents = 0; - epi->ovf_revents = 0; - epi->event = *event; - EP_LIST_INIT_NODE (&epi->fllink); - epi->revents = 0; - epi->fd = fdInfo->fd; - - /* Add the current item to the list of active epoll hook for this file - This should lock because file descriptor may be called by other eventpoll */ - - sys_arch_lock_with_pid (&fdInfo->epiLock); - ep_list_add_tail (&fdInfo->epiList, &epi->fllink); - epi->private_data = (void *) ADDR_LTOSH_EXT (fdInfo); - sys_sem_s_signal (&fdInfo->epiLock); - - /* Add epitem to eventpoll fd list, don't need lock here because epoll_ctl will lock before calling this function */ - nsep_rbtree_insert (ep, epi); - - /* Need to poll the events already stored in stack */ - nsep_epctl_triggle (epi, fdInfo, nstack_ep_triggle_add); - - NSSOC_LOGINF ("epfd=%d,ep=%p,fd=%d,epi=%p", ep->epfd, ep, fdInfo->fd, epi); - - return 0; -} - -NSTACK_STATIC int -nsep_isAddValid (int fd) -{ - - if (nstack_fix_fd_check ()) - { - return nstack_fix_fd_check ()(fd, STACK_FD_INVALID_CHECK); - } - return 0; -} - -int -nsep_epctl_add (struct eventpoll *ep, int fd, struct epoll_event *events) -{ - - int ret = 0; - - NSSOC_LOGINF ("epfd=%d,fd=%d,events=%u", ep->epfd, fd, events->events); - - nsep_epollInfo_t *fdInfo = nsep_get_infoBySock (fd); - - if (NULL == fdInfo) - { - if (0 == nsep_isAddValid (fd)) - { - NSSOC_LOGERR ("Invalid add check nfd=%d]", fd); - return -1; - } - if (-1 == nsep_alloc_infoWithSock (fd)) - { - NSSOC_LOGERR ("Can't alloc epInfo for nfd]nfd=%d", fd); - return -1; - } - nsep_set_infoProtoFD (fd, nstack_get_fix_mid (), fd); - fdInfo = nsep_get_infoBySock (fd); - } - - ret = nsep_insert_node (ep, events, fdInfo); - if (0 != ret) - { - NSSOC_LOGWAR ("insert fail]epfd=%d,fd=%d ", ep->epfd, fdInfo->fd); - return -1; - } - - return 0; -} - -int -nsep_epctl_del (struct eventpoll *ep, struct epitem *epi) -{ - int ret = 0; - - nsep_epollInfo_t *epInfo = - (nsep_epollInfo_t *) ADDR_SHTOL (epi->private_data); - NSSOC_LOGINF ("epfd=%d,fd=%d,epi=%p", ep->epfd, epi->fd, epi); // modify log format error - - nsep_epctl_triggle (epi, epInfo, nstack_ep_triggle_del); - - sys_arch_lock_with_pid (&epInfo->epiLock); - ep_list_del (&epInfo->epiList, &epi->fllink); - - sys_sem_s_signal (&epInfo->epiLock); - - sys_arch_lock_with_pid (&ep->lock); - ret = nstack_ep_unlink (ep, epi); - sys_sem_s_signal (&ep->lock); - nsep_free_epitem (epi); - - return ret; -} - -int -nsep_epctl_mod (struct eventpoll *ep, - nsep_epollInfo_t * epInfo, - struct epitem *epi, struct epoll_event *events) -{ - if (NULL == epInfo) - { - errno = EINVAL; - NSSOC_LOGWAR ("epfd=%d, intput epInfo is null err", ep->epfd); - return -1; - } - - NSSOC_LOGINF ("epfd=%d,fd=%d,events=%u", ep->epfd, epInfo->fd, - events->events); - - sys_arch_lock_with_pid (&ep->lock); - epi->event = *events; /* kernel tells me that I need to modify epi->event in lock context */ - sys_sem_s_signal (&ep->lock); - - nsep_epctl_triggle (epi, epInfo, nstack_ep_triggle_mod); - return 0; -} - -/* - * Called by nsep_ep_poll - * proccess the event in the list - */ -static int -nsep_events_proc (struct eventpoll *ep, - struct ep_node_list *nhead, - struct epoll_event *events, int maxevents, int *eventflag, - int num) -{ - struct ep_hlist_node *node = NULL; - struct epitem *epi = NULL; - struct ep_hlist_node *head = nhead->head; - int evt = 0; - - while (head) - { - node = head; - epi = ep_hlist_entry (node, struct epitem, rdllink); - head = (struct ep_hlist_node *) ADDR_SHTOL (node->next); - EP_HLIST_INIT_NODE (node); - - nsep_epollInfo_t *fdInfo = - (nsep_epollInfo_t *) ADDR_SHTOL (epi->private_data); - - if (fdInfo->rmidx != -1) - { - nstack_ep_getEvt (ep->epfd, fdInfo->rlfd, fdInfo->rmidx, - epi->event.events, (epi->revents)); - } - - if (epi->revents) - { - /*set the flag that already have event int the rdlist */ - if (eventflag && (fdInfo->rmidx >= 0) && (fdInfo->rmidx < num)) - { - eventflag[fdInfo->rmidx] = 1; - } - events[evt].events = epi->revents; - events[evt].data = epi->event.data; - NSSOC_LOGDBG ("Add event]epfd=%d,fd=%d,events=%u", ep->epfd, - epi->fd, events[evt].events); - evt++; - } - - if (0 != epi->revents && EPOLLET != (epi->event.events & EPOLLET)) - { - NSSOC_LOGDBG ("Add epi->rdllink,epfd=%d,fd=%d", ep->epfd, epi->fd); - ep_hlist_add_tail (&ep->rdlist, &epi->rdllink); - } - epi->revents = 0; - - if (evt >= maxevents) - break; - } - nhead->head = head; - if (!head) - { - nhead->tail = NULL; - } - return evt; -} - -/* - * Called by epoll_wait - * Wait until events come or timeout - */ -int -nsep_ep_poll (struct eventpoll *ep, struct epoll_event *events, int maxevents, - int *eventflag, int num) -{ - - struct ep_hlist_node *tail = NULL; - struct ep_hlist_node *head = NULL; - struct epitem *epi = NULL; - struct epitem *nepi = NULL; - struct ep_node_list nhead = { 0 }; - int evt = 0; - nsep_epollInfo_t *fdInfo = NULL; - - if (EP_HLIST_EMPTY (&ep->rdlist)) - { - NSSOC_LOGDBG ("ep->rdlist is Empty, epfd=%d", ep->epfd); - return 0; - } - - sys_arch_lock_with_pid (&ep->sem); - - if (EP_HLIST_EMPTY (&ep->rdlist)) - { - goto out; - } - - sys_arch_lock_with_pid (&ep->lock); - head = (struct ep_hlist_node *) ADDR_SHTOL (ep->rdlist.head); - if (!head) - { - sys_sem_s_signal (&ep->lock); - goto out; - } - nhead.head = (struct ep_hlist_node *) ADDR_SHTOL (head->next); - nhead.tail = (struct ep_hlist_node *) ADDR_SHTOL (ep->rdlist.tail); - /*unlink from ep->rdlist */ - EP_HLIST_INIT (&(ep->rdlist)); - ep->ovflist = NULL; - sys_sem_s_signal (&ep->lock); - - /*get event list */ - evt = nsep_events_proc (ep, &nhead, events, maxevents, eventflag, num); - - sys_arch_lock_with_pid (&ep->lock); - /*put rest epitem back to the rdlist */ - if (nhead.head) - { - tail = (struct ep_hlist_node *) ADDR_SHTOL (ep->rdlist.tail); - tail->next = (struct ep_hlist_node *) ADDR_LTOSH (nhead.head); - nhead.head->pprev = (struct ep_hlist_node **) ADDR_LTOSH (&tail->next); - ep->rdlist.tail = (struct ep_hlist_node *) ADDR_LTOSH (nhead.tail); - } - /*put the epitem in ep->ovflist to rdlist */ - for (nepi = (struct epitem *) ADDR_SHTOL (ep->ovflist); - (epi = nepi) != NULL; - nepi = (struct epitem *) ADDR_SHTOL (epi->next), epi->next = - NSEP_EP_UNACTIVE_PTR) - { - epi->revents |= epi->ovf_revents; - /*set the flag that already have event int the rdlist */ - fdInfo = (nsep_epollInfo_t *) ADDR_SHTOL (epi->private_data); - if (eventflag && fdInfo && (fdInfo->rmidx >= 0) - && (fdInfo->rmidx < num)) - { - eventflag[fdInfo->rmidx] = 1; - } - epi->ovf_revents = 0; - if (!EP_HLIST_NODE_LINKED (&epi->rdllink)) - { - ep_hlist_add_tail (&ep->rdlist, &epi->rdllink); - } - } - ep->ovflist = NSEP_EP_UNACTIVE_PTR; - sys_sem_s_signal (&ep->lock); -out: - sys_sem_s_signal (&ep->sem); - NSSOC_LOGDBG ("Return epfd=%d,evt=%d,EP_HLIST_EMPTY(&ep->rdlist)=%d", - ep->epfd, evt, EP_HLIST_EMPTY (&ep->rdlist)); - return evt; -} - -#define FREE_LIST_SIZE 128 -#ifdef FREE_LIST_SIZE -struct free_list -{ - struct free_list *next; - struct list_node *node[FREE_LIST_SIZE]; -}; -#endif - -void -nsep_remove_epfd (nsep_epollInfo_t * pinfo) -{ - pid_t pid = get_sys_pid (); - struct list_node *prenode = NULL; - struct list_node *nextnode = NULL; -#ifdef FREE_LIST_SIZE - struct free_list flist; - struct free_list *fcurr = &flist; -#else - struct list_node **node_arry = NULL; - int length = NSTACK_MAX_EPOLL_INFO_NUM * sizeof (struct list_node *); -#endif - struct epitem *epi = NULL; - struct epitem *tepi = NULL; - struct eventpoll *ep = NULL; - u32_t i = 0; - u32_t icnt = 0; - - if (!pinfo) - { - return; - } - /*malloc a block memory to store epitem node, do not use list for maybe free item */ -#ifdef FREE_LIST_SIZE - flist.next = 0; -#else - node_arry = (struct list_node **) malloc (length); - if (!node_arry) - { - NSSOC_LOGERR ("remove fd from ep malloc mem fail]fd=%d,ep=%p", - pinfo->fd, pinfo->ep); - return; - } - - int retVal = MEMSET_S (node_arry, length, 0, length); - if (EOK != retVal) - { - NSSOC_LOGERR ("MEMSET_S failed]retVal=%d", retVal); - free (node_arry); - return; - } -#endif - sys_arch_lock_with_pid (&pinfo->epiLock); - /*list head must be not null */ - prenode = (struct list_node *) ADDR_SHTOL (pinfo->epiList.head); - nextnode = (struct list_node *) ADDR_SHTOL (prenode->next); - icnt = 0; - - /*find all node that pid is belong to itself */ - while (nextnode) - { - if (++i > NSTACK_MAX_EPOLL_INFO_NUM) - { - /*record the exception log */ - NSSOC_LOGERR ("error maybe happen]free pinfo=%p", pinfo); - break; - } - epi = ep_list_entry (nextnode, struct epitem, fllink); - if (pid == epi->pid) - { - prenode->next = nextnode->next; - nextnode->next = NULL; - /*put into release list */ -#ifdef FREE_LIST_SIZE - if ((icnt % FREE_LIST_SIZE) == 0 && icnt) - { - struct free_list *fnext = - (struct free_list *) malloc (sizeof (struct free_list)); - if (!fnext) - { - NSSOC_LOGERR ("Out of memory for freelist]fd=%d icnt=%u", - pinfo->fd, icnt); - break; - } - fnext->next = NULL; - fcurr->next = fnext; - fcurr = fnext; - } - fcurr->node[icnt % FREE_LIST_SIZE] = nextnode; -#else - node_arry[icnt] = nextnode; -#endif - icnt++; - } - else - { - prenode = nextnode; - } - nextnode = (struct list_node *) ADDR_SHTOL (prenode->next); - } - - sys_sem_s_signal (&pinfo->epiLock); - - /*free all epitem */ -#ifdef FREE_LIST_SIZE - fcurr = &flist; -#endif - for (i = 0; i < icnt; i++) - { -#ifdef FREE_LIST_SIZE - if ((i % FREE_LIST_SIZE) == 0 && i) - { - fcurr = fcurr->next; - if (fcurr == NULL) - { - NSSOC_LOGERR ("freelist invalid NULL, fd=%d icnt=%u i=%u", - pinfo->fd, icnt, i); - break; - } - } - epi = - ep_list_entry (fcurr->node[i % FREE_LIST_SIZE], struct epitem, - fllink); -#else - epi = ep_list_entry (node_arry[i], struct epitem, fllink); -#endif - ep = (struct eventpoll *) ADDR_SHTOL (epi->ep); - if (ep) - { - sys_arch_lock_with_pid (&ep->sem); - /* Here don't use epi you find before, use fd and ep to find the epi again.that is multithread safe */ - tepi = nsep_find_ep (ep, pinfo->fd); - /*record the exception log */ - if (epi != tepi) - { - NSSOC_LOGERR ("remove fd:%d epi:%p tepi:%p erro maybe happen", - pinfo->fd, epi, tepi); - } - /*if tepi is null, epi maybe free by nsep_close_epfd, so no need to free again */ - if (tepi) - { - nsep_epctl_triggle (tepi, pinfo, nstack_ep_triggle_del); - sys_arch_lock_with_pid (&ep->lock); - (void) nstack_ep_unlink (ep, tepi); - sys_sem_s_signal (&ep->lock); - - nsep_free_epitem (epi); - } - sys_sem_s_signal (&ep->sem); - } - } -#ifdef FREE_LIST_SIZE - for (fcurr = flist.next; fcurr; fcurr = flist.next) - { - flist.next = fcurr->next; - free (fcurr); - } -#else - free (node_arry); -#endif - return; -} - -void -nsep_close_epfd (struct eventpoll *ep) -{ - - if (!ep) - return; - - struct epitem *epi = NULL; - struct ep_rb_node *node = NULL; - - sys_arch_lock_with_pid (&ep->sem); - while ((node = ep_rb_first (&ep->rbr))) - { - - epi = ep_rb_entry (node, struct epitem, rbn); - int ret = nsep_epctl_del (ep, epi); - - if (ret) - { - NSSOC_LOGERR - ("nstack epctl del fail, will break to avoid dead loop]ep->fd=%d,epi->fd=%d", - ep->epfd, epi->fd); - break; - } - } - sys_sem_s_signal (&ep->sem); - nsep_free_eventpoll (ep); -} - -static inline int -nsp_epoll_close_kernel_fd (int sock, nsep_epollInfo_t * epInfo) -{ - NSSOC_LOGINF ("fd=%d,type=%d", sock, epInfo->fdtype); - int ret = 0; - nsep_remove_epfd (epInfo); - - u32_t pid = get_sys_pid (); - sys_arch_lock_with_pid (&epInfo->freeLock); - int left_count = nsep_del_last_pid (&epInfo->pidinfo, pid); - sys_sem_s_signal (&epInfo->freeLock); - if (-1 == left_count) - { - NSSOC_LOGERR ("pid not exist]fd=%d,type=%d,pid=%d", sock, - epInfo->fdtype, pid); - } - - if (0 == left_count) - { - ret = nsep_free_epinfo (epInfo); - NSSOC_LOGINF ("epinfo removed]fd=%d,type=%d", sock, epInfo->fdtype); - } - - return ret; -} - -static inline int -nsp_epoll_close_spl_fd (int sock, nsep_epollInfo_t * epInfo) -{ - NSSOC_LOGINF ("fd=%d,type=%d", sock, epInfo->fdtype); - nsep_remove_epfd (epInfo); - return 0; -} - -static inline int -nsp_epoll_close_ep_fd (int sock, nsep_epollInfo_t * epInfo) -{ - struct eventpoll *ep = ADDR_SHTOL (epInfo->ep); - u32_t pid = get_sys_pid (); - NSSOC_LOGINF ("fd:%d is epoll fd ep:%p]", sock, ep); - sys_arch_lock_with_pid (&epInfo->freeLock); - int left_count = nsep_del_last_pid (&epInfo->pidinfo, pid); - sys_sem_s_signal (&epInfo->freeLock); - if (0 == left_count) - { - epInfo->ep = NULL; - nsep_close_epfd (ep); - nsep_free_epinfo (epInfo); - } - return 0; -} - -int -nsep_epoll_close (int sock) -{ - int ret = 0; - int modInx = 0; - int flag = 0; - nsep_epollInfo_t *epInfo = nsep_get_infoBySock (sock); - if (!epInfo) - { - NSSOC_LOGDBG ("epollsock close sock:%d is not exist", sock); - return 0; - } - - if (NSTACK_EPOL_FD == epInfo->fdtype) - { - ret = nsp_epoll_close_ep_fd (sock, epInfo); - nsep_set_infoSockMap (sock, NULL); - return ret; - } - - nsep_set_infoSockMap (sock, NULL); - - nstack_each_modInx (modInx) - { - if (0 == (epInfo->epaddflag & (1 << modInx))) - { - flag = 1; - } - } - /*if no stack reference the epinfo, just free epInfo, - *else wait until the stack closed completely - */ - if (0 == flag) - { - ret = nsp_epoll_close_kernel_fd (sock, epInfo); - } - else - { - ret = nsp_epoll_close_spl_fd (sock, epInfo); - } - return ret; -} - -void -nsep_set_infoSockMap (int sock, nsep_epollInfo_t * info) -{ - nsep_epollManager_t *manager = nsep_getManager (); - if (NULL == manager->infoSockMap) - return; - - if (sock < 0 || (u32_t) sock >= NSTACK_KERNEL_FD_MAX) - return; - - manager->infoSockMap[sock] = info; -} - -nsep_epollInfo_t * -nsep_get_infoBySock (int sock) -{ - nsep_epollManager_t *manager = nsep_getManager (); - if ((NULL == manager) || (NULL == manager->infoSockMap)) - return NULL; - - if (sock < 0 || (u32_t) sock >= NSTACK_KERNEL_FD_MAX) - return NULL; - - return manager->infoSockMap[sock]; -} - -int -nsep_alloc_infoWithSock (int nfd) -{ - - nsep_epollInfo_t *epInfo = NULL; - - if (!NSEP_IS_SOCK_VALID (nfd)) - { - return -1; - } - - if (-1 == nsep_alloc_epinfo (&epInfo)) - { - NSSOC_LOGERR ("Alloc share info fail,[return]"); - return -1; - } - - epInfo->fd = nfd; - - nsep_set_infoSockMap (nfd, epInfo); - - return 0; -} - -void -nsep_set_infoProtoFD (int fd, int modInx, int protoFD) -{ - nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd); - - if (NULL == epInfo) - return; - - if (modInx < 0 || modInx >= NSTACK_MAX_MODULE_NUM) - return; - - epInfo->protoFD[modInx] = protoFD; -} - -int -nsep_get_infoProtoFD (int fd, int modInx) -{ - nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd); - - if (NULL == epInfo) - return -1; - - return epInfo->protoFD[modInx]; -} - -void -nsep_set_infomdix (int fd, int rmidx) -{ - nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd); - - if (NULL == epInfo) - return; - - epInfo->rmidx = rmidx; -} - -int -nsep_get_infoMidx (int fd) -{ - nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd); - - if (NULL == epInfo) - return -1; - - return epInfo->rmidx; -} - -void -nsep_set_infoRlfd (int fd, int rlfd) -{ - nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd); - - if (NULL == epInfo) - return; - - epInfo->rlfd = rlfd; -} - -int -nsep_get_infoRlfd (int fd) -{ - nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd); - - if (NULL == epInfo) - return -1; - - return epInfo->rlfd; -} - -void -nsep_set_infoSleepTime (int fd, u32 sleepTime) -{ - nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd); - - if (NULL == epInfo) - return; - - epInfo->sleepTime = sleepTime; -} - -int -nsep_get_infoSleepTime (int fd) -{ - nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd); - - if (NULL == epInfo) - return -1; - - return epInfo->sleepTime; -} - -void -nsep_set_infoEp (int fd, struct eventpoll *ep) -{ - nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd); - - if (NULL == epInfo) - return; - - epInfo->ep = (struct eventpoll *) ADDR_LTOSH (ep); - epInfo->fdtype = NSTACK_EPOL_FD; -} - -struct eventpoll * -nsep_get_infoEp (int fd) -{ - nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd); - - if (NULL == epInfo) - return NULL; - - return (struct eventpoll *) ADDR_SHTOL (epInfo->ep); -} - -int -nsep_free_infoWithSock (int nfd) -{ - if ((u32_t) nfd >= NSTACK_KERNEL_FD_MAX || nfd < 0) - return -1; - - nsep_epollInfo_t *info = nsep_get_infoBySock (nfd); - - if (NULL == info) - return 0; - - nsep_set_infoSockMap (nfd, NULL); - - NSSOC_LOGDBG ("nsep_free_infoWithSock info:%p, nfd:%d", info, nfd); - /* If this not just used by linux, it should be freed in stack-x */ - if (-1 == nsep_free_epinfo (info)) - { - NSSOC_LOGERR ("Error to free ep info"); - return -1; - } - return 0; -} - -/** - * @Function nsep_init_infoSockMap - * @Description initial map of epoll info and socket - * @param none - * @return 0 on success, -1 on error - */ -int -nsep_init_infoSockMap () -{ - nsep_epollManager_t *manager = nsep_getManager (); - nsep_epollInfo_t **map = - (nsep_epollInfo_t **) malloc (NSTACK_KERNEL_FD_MAX * - sizeof (nsep_epollInfo_t *)); - - if (!map) - { - NSSOC_LOGERR ("malloc epInfoPool fail"); - return -1; - } - - u32_t iindex; - for (iindex = 0; iindex < NSTACK_KERNEL_FD_MAX; iindex++) - { - map[iindex] = NULL; - } - - manager->infoSockMap = map; - - return 0; -} - -NSTACK_STATIC mzone_handle -nsep_ring_lookup (char *name) -{ - if (NULL == name) - { - NSSOC_LOGERR ("param error]name=%p", name); - return NULL; - } - - nsfw_mem_name mem_name; - if (EOK != STRCPY_S (mem_name.aname, sizeof (mem_name.aname), name)) - { - NSSOC_LOGERR ("Error to lookup ring by name, strcpy fail]name=%s", - name); - return NULL; - } - mem_name.enowner = NSFW_PROC_MAIN; - mem_name.entype = NSFW_SHMEM; - - return nsfw_mem_ring_lookup (&mem_name); -} - -NSTACK_STATIC mzone_handle -nsep_attach_shmem (char *name) -{ - if (NULL == name) - { - NSSOC_LOGERR ("param error]name=%p", name); - return NULL; - } - - nsfw_mem_name mem_name; - int retVal = STRCPY_S (mem_name.aname, sizeof (mem_name.aname), name); - if (EOK != retVal) - { - NSSOC_LOGERR ("STRCPY_S failed]"); - return NULL; - } - mem_name.enowner = NSFW_PROC_MAIN; - mem_name.entype = NSFW_SHMEM; - - return nsfw_mem_zone_lookup (&mem_name); -} - -NSTACK_STATIC int -nsep_attach_infoMem () -{ - mzone_handle hdl = nsep_attach_shmem (MP_NSTACK_EPOLL_INFO_NAME); - if (NULL == hdl) - return -1; - - nsep_epollManager_t *manager = nsep_getManager (); - manager->infoPool.pool = (nsep_epollInfo_t *) hdl; - - hdl = nsep_ring_lookup (MP_NSTACK_EPINFO_RING_NAME); - if (NULL == hdl) - { - NSSOC_LOGERR ("Fail to lock up epoll info ring]name=%s", - MP_NSTACK_EPINFO_RING_NAME); - return -1; - } - - manager->infoPool.ring = hdl; - - return 0; -} - -NSTACK_STATIC int -nsep_attach_epItemMem () -{ - mzone_handle hdl = nsep_attach_shmem (MP_NSTACK_EPITEM_POOL); - if (NULL == hdl) - return -1; - - nsep_epollManager_t *manager = nsep_getManager (); - manager->epitemPool.pool = (struct epitem *) hdl; - - hdl = nsep_ring_lookup (MP_NSTACK_EPITEM_RING_NAME); - if (NULL == hdl) - { - NSSOC_LOGERR ("Fail to lock up epoll info ring]name=%s", - MP_NSTACK_EPITEM_RING_NAME); - return -1; - } - - manager->epitemPool.ring = hdl; - - return 0; -} - -NSTACK_STATIC int -nsep_attach_eventpollMem () -{ - mzone_handle hdl = nsep_attach_shmem (MP_NSTACK_EVENTPOLL_POOL); - if (NULL == hdl) - return -1; - - nsep_epollManager_t *manager = nsep_getManager (); - manager->epollPool.pool = (struct eventpoll *) hdl; - - hdl = nsep_ring_lookup (MP_NSTACK_EVENTPOOL_RING_NAME); - if (NULL == hdl) - { - NSSOC_LOGERR ("Fail to lock up epoll info ring]name=%s", - MP_NSTACK_EVENTPOOL_RING_NAME); - return -1; - } - - manager->epollPool.ring = hdl; - - return 0; -} - -/* epinfo add pid in parent */ -void -nsep_fork_parent_proc (u32_t ppid, u32_t cpid) -{ - nsep_epollManager_t *manager = nsep_getManager (); - if (NULL == manager->infoSockMap) - { - NSSOC_LOGERR ("infoSockMap is NULL]ppid=%d,cpid=%d", ppid, cpid); - return; - } - - nstack_fd_Inf *fdInf = NULL; - nsep_epollInfo_t *epinfo = NULL; - int pos; - for (pos = 0; (u32_t) pos < NSTACK_KERNEL_FD_MAX; pos++) - { - epinfo = manager->infoSockMap[pos]; - if (epinfo) - { - fdInf = nstack_getValidInf (pos); - if (fdInf) - { - if (((u32_t) (fdInf->type)) & SOCK_CLOEXEC) - { - NSSOC_LOGINF ("fd is SOCK_CLOEXEC]fd=%d,ppid=%d.cpid=%d", - pos, ppid, cpid); - continue; - } - } - - if (nsep_add_pid (&epinfo->pidinfo, cpid) != 0) - { - NSSOC_LOGERR ("epinfo add pid failed]fd=%d,ppid=%d.cpid=%d", - pos, ppid, cpid); - } - else - { - NSSOC_LOGDBG ("epinfo add pid ok]fd=%d,ppid=%d.cpid=%d", pos, - ppid, cpid); - } - } - } -} - -/* check is pid exist in child,if no,detach epinfo*/ -void -nsep_fork_child_proc (u32_t ppid) -{ - nsep_epollManager_t *manager = nsep_getManager (); - if (NULL == manager->infoSockMap) - { - NSSOC_LOGERR ("epinfi sockmap not be create"); - return; - } - - u32_t cpid = get_sys_pid (); - nsep_epollInfo_t *epinfo = NULL; - int pos; - for (pos = 0; (u32_t) pos < NSTACK_KERNEL_FD_MAX; pos++) - { - epinfo = manager->infoSockMap[pos]; - if (epinfo) - { - if (!nsep_is_pid_exist (&epinfo->pidinfo, cpid)) - { - NSSOC_LOGINF - ("unuse epinfo,happen in SOCK_CLOEXEC,!FD_OPEN,parent coredump]fd=%d,epinfo=%p,ppid=%d,cpid=%d", - pos, epinfo, ppid, cpid); - nsep_set_infoSockMap (pos, NULL); - } - } - } -} - -int -nsep_attach_memory () -{ - typedef int (*nsep_attachMemFunc_t) (void); - nsep_attachMemFunc_t attachFuncs[] = { nsep_attach_infoMem, - nsep_attach_epItemMem, - nsep_attach_eventpollMem - }; - - int i = 0; - for (i = 0; - i < (int) (sizeof (attachFuncs) / sizeof (nsep_attachMemFunc_t)); i++) - { - if (-1 == attachFuncs[i] ()) - return -1; - } - - return 0; -} - -#ifdef __cplusplus -/* *INDENT-OFF* */ -} -/* *INDENT-ON* */ -#endif /* __cplusplus */ diff --git a/src/nSocket/nstack/event/select/nstack_select.c b/src/nSocket/nstack/event/select/nstack_select.c index f61b326..b9db0d7 100644 --- a/src/nSocket/nstack/event/select/nstack_select.c +++ b/src/nSocket/nstack/event/select/nstack_select.c @@ -20,220 +20,231 @@ #include "nstack_select.h" #include "nstack_log.h" #include "nsfw_base_linux_api.h" - +#include "nstack.h" +#include "nstack_dmm_dfx.h" /*==============================================* * constants or macros define * *----------------------------------------------*/ #ifdef NSTACK_SELECT_MODULE +#define SELECT_FREE_FD_BITS(read_set, write_set, exp_set) do{\ + select_free((read_set)->fds_bits); \ + select_free((write_set)->fds_bits); \ + select_free((exp_set)->fds_bits); \ +}while(0) /*==============================================* * project-wide global variables * *----------------------------------------------*/ -extern void *nstack_select_thread (void *arg); +extern void *nstack_select_thread(void *arg); +pthread_t g_select_thread_id; /*************select module***************************/ struct select_module_info g_select_module = { - .inited = FALSE, + .inited = FALSE, }; -/*==============================================* - * routines' or functions' implementations * - *----------------------------------------------*/ -/***************************************************************************** -* Prototype : get_select_module -* Description : get_select_module -* Input : void -* Output : None -* Return Value : struct select_module_info * -* Calls : -* Called By : -*****************************************************************************/ -struct select_module_info * -get_select_module (void) +struct select_module_info *get_select_module(void) { - return &g_select_module; + return &g_select_module; } /*split comm seclet entry to child mod select*/ -/***************************************************************************** -* Prototype : select_cb_split_by_mod -* Description : select_cb_split_by_module -* Input : i32 nfds -* fd_set *readfd -* fd_set *writefd -* fd_set *exceptfd -* struct select_entry *entry -* Output : None -* Return Value : i32 -* Calls : -* Called By : -*****************************************************************************/ -i32 -select_cb_split_by_mod (i32 nfds, - fd_set * readfd, - fd_set * writefd, - fd_set * exceptfd, struct select_entry * entry) +/*no need to check null pointer*/ + +i32 select_cb_split_by_mod(i32 nfds, + fd_set * readfd, + fd_set * writefd, + fd_set * exceptfd, struct select_entry * entry) { - i32 inx; - i32 i; - i32 fd; + i32 inx; + i32 i; + i32 fd; - for (i = 0; i < nfds; i++) + for (i = 0; i < nfds; i++) { - - /*not bound to any stack */ - for (inx = 0; inx < get_mode_num (); inx++) + /*not bound to any stack */ + for (inx = 0; inx < nstack_get_module_num(); inx++) { - if (!((readfd && FD_ISSET (i, readfd)) || - (writefd && FD_ISSET (i, writefd)) || - (exceptfd && FD_ISSET (i, exceptfd)))) + if (!((readfd && FD_ISSET(i, readfd)) || + (writefd && FD_ISSET(i, writefd)) || + (exceptfd && FD_ISSET(i, exceptfd)))) { - continue; + continue; } - fd = select_get_modfd (i, inx); - /*not create by nstack */ - if ((fd < 0) || (select_get_modindex (i) < 0)) + fd = select_get_modfd(i, inx); + /*not create by nstack */ + if ((fd < 0) || (select_get_modindex(i) < 0)) { - - if (inx != get_mode_linux_index ()) + if (inx != nstack_get_linux_mid()) { - continue; + continue; } - fd = i; - nssct_create (fd, fd, inx); + fd = i; + nssct_create(fd, fd, inx); /*do not need return value */ } - else + else { - if (select_get_modindex (i) != inx) - continue; + if (select_get_modindex(i) != inx) + { + continue; + } } - NSSOC_LOGDBG ("fd is available i= %d fd = %d index = %d\n", i, fd, - inx); - if ((readfd) && (FD_ISSET (i, readfd))) + + NSSOC_LOGDBG("fd is valiable i= %d fd = %d index = %d\n", i, fd, + inx); + if ((readfd) && (FD_ISSET(i, readfd))) { - NSTACK_FD_SET (fd, &(entry->cb[inx].nstack_readset)); - if (entry->cb[inx].count <= fd) - entry->cb[inx].count = fd + 1; + if (inx == nstack_get_linux_mid()) + { + FD_SET(fd, &(entry->cb[inx].readset)); + } + else + { + NSTACK_FD_SET(fd, &(entry->cb[inx].nstack_readset)); + } + if (entry->cb[inx].count <= fd) + { + entry->cb[inx].count = fd + 1; + } } - if ((writefd) && (FD_ISSET (i, writefd))) + if ((writefd) && (FD_ISSET(i, writefd))) { - NSTACK_FD_SET (fd, &(entry->cb[inx].nstack_writeset)); - if (entry->cb[inx].count <= fd) - entry->cb[inx].count = fd + 1; + if (inx == nstack_get_linux_mid()) + { + FD_SET(fd, &(entry->cb[inx].writeset)); + } + else + { + NSTACK_FD_SET(fd, &(entry->cb[inx].nstack_writeset)); + } + + if (entry->cb[inx].count <= fd) + { + entry->cb[inx].count = fd + 1; + } } - if ((exceptfd) && (FD_ISSET (i, exceptfd))) + if ((exceptfd) && (FD_ISSET(i, exceptfd))) { - NSTACK_FD_SET (fd, &(entry->cb[inx].nstack_exceptset)); - if (entry->cb[inx].count <= fd) - entry->cb[inx].count = fd + 1; + if (inx == nstack_get_linux_mid()) + { + FD_SET(fd, &(entry->cb[inx].exceptset)); + } + else + { + NSTACK_FD_SET(fd, &(entry->cb[inx].nstack_exceptset)); + } + + if (entry->cb[inx].count <= fd) + { + entry->cb[inx].count = fd + 1; + } } } } - for (inx = 0; inx < get_mode_num (); inx++) + for (inx = 0; inx < nstack_get_module_num(); inx++) { - if (entry->cb[inx].count > 0) + if (entry->cb[inx].count > 0) { - entry->info.set_num++; - entry->info.index = inx; + entry->info.set_num++; + entry->info.index = inx; } } - return TRUE; + return TRUE; } /***************************************************************************** * Prototype : select_add_cb -* Description : add cb to global list +* Description : add cb to gloab list * Input : struct select_entry *entry * Output : None * Return Value : i32 * Calls : * Called By : *****************************************************************************/ -i32 -select_add_cb (struct select_entry * entry) +i32 select_add_cb(struct select_entry * entry) { - if ((!entry)) + if ((!entry)) { - return FALSE; + return FALSE; } - select_spin_lock (&g_select_module.lock); + select_spin_lock(&g_select_module.lock); - if (!g_select_module.entry_head) + if (!g_select_module.entry_head) { - g_select_module.entry_head = entry; - g_select_module.entry_tail = entry; - entry->next = NULL; - entry->prev = NULL; + g_select_module.entry_head = entry; + g_select_module.entry_tail = entry; + entry->next = NULL; + entry->prev = NULL; } - else + else { - g_select_module.entry_tail->next = entry; - entry->prev = g_select_module.entry_tail; - g_select_module.entry_tail = entry; - entry->next = NULL; + g_select_module.entry_tail->next = entry; + entry->prev = g_select_module.entry_tail; + g_select_module.entry_tail = entry; + entry->next = NULL; } - select_spin_unlock (&g_select_module.lock); - select_sem_post (&g_select_module.sem); - return TRUE; + select_spin_unlock(&g_select_module.lock); + select_sem_post(&g_select_module.sem); /*do not need return value */ + return TRUE; } /***************************************************************************** * Prototype : select_rm_cb -* Description : rm the cb from global list +* Description : rm the cb from gloab list * Input : struct select_entry *entry * Output : None * Return Value : i32 * Calls : * Called By : *****************************************************************************/ -i32 -select_rm_cb (struct select_entry * entry) +i32 select_rm_cb(struct select_entry * entry) { - if (!entry) + if (!entry) { - return FALSE; + return FALSE; } - select_spin_lock (&g_select_module.lock); + select_spin_lock(&g_select_module.lock); - if (g_select_module.entry_head == entry) + if (g_select_module.entry_head == entry) { - g_select_module.entry_head = entry->next; + g_select_module.entry_head = entry->next; } - else if (entry->prev) + else if (entry->prev) { - entry->prev->next = entry->next; + entry->prev->next = entry->next; } - if (g_select_module.entry_tail == entry) + if (g_select_module.entry_tail == entry) { - g_select_module.entry_tail = entry->prev; + g_select_module.entry_tail = entry->prev; } - else if (entry->next) + else if (entry->next) { - entry->next->prev = entry->prev; + entry->next->prev = entry->prev; } - entry->next = NULL; - entry->prev = NULL; + entry->next = NULL; + entry->prev = NULL; - select_spin_unlock (&g_select_module.lock); - return TRUE; + select_spin_unlock(&g_select_module.lock); + return TRUE; } -/*get fd set from entry*/ +/*get fd set from entrys*/ +/*no need to check null pointer*/ /***************************************************************************** * Prototype : select_thread_get_fdset -* Description : get module listening fd form global list +* Description : get module listening fd form gloab list * Input : nstack_fd_set *readfd * nstack_fd_set *writefd * nstack_fd_set *exceptfd @@ -244,55 +255,118 @@ select_rm_cb (struct select_entry * entry) * Calls : * Called By : *****************************************************************************/ -i32 -select_thread_get_fdset (nstack_fd_set * readfd, - nstack_fd_set * writefd, - nstack_fd_set * exceptfd, - struct select_module_info * module, i32 inx) +i32 select_thread_get_fdset(nstack_fd_set * readfd, + nstack_fd_set * writefd, + nstack_fd_set * exceptfd, + struct select_module_info * module, i32 inx) { - struct select_entry *tmp; - i32 nfds = 0; - int retVal; + struct select_entry *tmp; + i32 nfds = 0; + int retVal; - if (!module) + if (!module) { - return FALSE; + return FALSE; } - retVal = NSTACK_FD_ZERO (readfd); - retVal |= NSTACK_FD_ZERO (writefd); - retVal |= NSTACK_FD_ZERO (exceptfd); - if (EOK != retVal) + u32 fd_set_size = + sizeof(unsigned char) * ((NSTACK_SELECT_MAX_FD + 7) / 8); + + /*add return value check */ + retVal = NSTACK_FD_ZERO(readfd, fd_set_size); + retVal |= NSTACK_FD_ZERO(writefd, fd_set_size); + retVal |= NSTACK_FD_ZERO(exceptfd, fd_set_size); + if (EOK != retVal) { - NSSOC_LOGERR ("NSTACK_FD_ZERO MEMSET_S failed]ret=%d", retVal); - return FALSE; + NSSOC_LOGERR("NSTACK_FD_ZERO memset_s failed]ret=%d", retVal); + return FALSE; } - select_spin_lock (&module->lock); - for (tmp = module->entry_head; NULL != tmp; tmp = tmp->next) + select_spin_lock(&module->lock); + for (tmp = module->entry_head; NULL != tmp; tmp = tmp->next) { - if (tmp->cb[inx].count <= 0) + if (tmp->cb[inx].count <= 0) { - continue; + continue; } - NSTACK_FD_OR (readfd, &tmp->cb[inx].nstack_readset); - NSTACK_FD_OR (writefd, &tmp->cb[inx].nstack_writeset); - NSTACK_FD_OR (exceptfd, &tmp->cb[inx].nstack_exceptset); - if (nfds < tmp->cb[inx].count) + NSTACK_FD_OR(readfd, &tmp->cb[inx].nstack_readset); + NSTACK_FD_OR(writefd, &tmp->cb[inx].nstack_writeset); + NSTACK_FD_OR(exceptfd, &tmp->cb[inx].nstack_exceptset); + if (nfds < tmp->cb[inx].count) { - nfds = tmp->cb[inx].count; + nfds = tmp->cb[inx].count; } } - select_spin_unlock (&module->lock); + select_spin_unlock(&module->lock); - return nfds; + return nfds; +} + +/*no need to check null pointer*/ + +i32 select_thread_get_fdset_linux(fd_set * readfd, + fd_set * writefd, + fd_set * exceptfd, + struct select_module_info * module, i32 inx) +{ + struct select_entry *tmp; + i32 nfds = 0; + int i; + + if (!module) + { + return 0; + } + + FD_ZERO(readfd); + FD_ZERO(writefd); + FD_ZERO(exceptfd); + + select_spin_lock(&module->lock); + + for (tmp = module->entry_head; NULL != tmp; tmp = tmp->next) + { + if (tmp->cb[inx].count <= 0) + { + continue; + } + + /*need to diff linux and daemon-stack */ + for (i = 0; i < __FD_SETSIZE; i++) + { + if (FD_ISSET(i, &tmp->cb[inx].readset)) + { + FD_SET(i, readfd); + } + + if (FD_ISSET(i, &tmp->cb[inx].writeset)) + { + FD_SET(i, writefd); + } + + if (FD_ISSET(i, &tmp->cb[inx].exceptset)) + { + FD_SET(i, exceptfd); + } + } + /*need to diff linux and daemon-stack */ + + if (nfds < tmp->cb[inx].count) + { + nfds = tmp->cb[inx].count; + } + } + + select_spin_unlock(&module->lock); + + return nfds; } /***************************************************************************** * Prototype : select_thread_set_fdset -* Description : set ready event to global list +* Description : set ready event to gloab list * Input : i32 nfds * nstack_fd_set *readfd * nstack_fd_set *writefd @@ -305,186 +379,402 @@ select_thread_get_fdset (nstack_fd_set * readfd, * Calls : * Called By : *****************************************************************************/ -i32 -select_thread_set_fdset (i32 nfds, - nstack_fd_set * readfd, - nstack_fd_set * writefd, - nstack_fd_set * exceptfd, - struct select_module_info * module, i32 inx, i32 err) +i32 select_thread_set_fdset(i32 nfds, + nstack_fd_set * readfd, + nstack_fd_set * writefd, + nstack_fd_set * exceptfd, + struct select_module_info * module, + i32 inx, i32 err) { - struct select_entry *tmp; + struct select_entry *tmp; - if (!module) + if (!module) { - return FALSE; + return FALSE; } - select_spin_lock (&module->lock); - for (tmp = module->entry_head; NULL != tmp; tmp = tmp->next) + select_spin_lock(&module->lock); + for (tmp = module->entry_head; NULL != tmp; tmp = tmp->next) { - if (tmp->cb[inx].count <= 0) + if (tmp->cb[inx].count <= 0) { - continue; + continue; } - if (nfds < 0) + if (nfds < 0) { - tmp->ready.readyset = nfds; - tmp->ready.select_errno = err; - continue; + tmp->ready.readyset = nfds; + tmp->ready.select_errno = err; + continue; } - NSSOC_LOGDBG ("readyset=%d,index=%d", tmp->ready.readyset, inx); - entry_module_fdset (tmp, nfds, readfd, writefd, exceptfd, inx); + NSSOC_LOGDBG("readyset=%d,index=%d", tmp->ready.readyset, inx); + entry_module_fdset(tmp, nfds, readfd, writefd, exceptfd, inx); } - select_spin_unlock (&module->lock); - return TRUE; + select_spin_unlock(&module->lock); + return TRUE; } +NSTACK_STATIC inline void entry_mod_fdset_linux(int fd, int idx, int inx, + struct select_entry *entry, + fd_set * readfd, + fd_set * writefd, + fd_set * exceptfd) +{ + if (FD_ISSET(idx, readfd) && FD_ISSET(idx, &entry->cb[inx].readset)) + { + FD_SET(fd, &entry->ready.readset); + entry->ready.count++; + NSSOC_LOGDBG("readyset is %d", entry->ready.readyset); + } + + if (FD_ISSET(idx, writefd) && FD_ISSET(idx, &entry->cb[inx].writeset)) + { + FD_SET(fd, &entry->ready.writeset); + entry->ready.count++; + NSSOC_LOGDBG("writeset is %d", entry->ready.readyset); + } + + if (FD_ISSET(idx, exceptfd) && FD_ISSET(idx, &entry->cb[inx].exceptset)) + { + FD_SET(fd, &entry->ready.exceptset); + entry->ready.count++; + NSSOC_LOGDBG("exceptset is %d", entry->ready.readyset); + } +} + +NSTACK_STATIC inline void entry_module_fdset_linux(struct select_entry + *entry, i32 fd_size, + fd_set * readfd, + fd_set * writefd, + fd_set * exceptfd, i32 inx) +{ + i32 i; + i32 fd; + + for (i = 0; i < fd_size; i++) + { + fd = select_get_commfd(i, inx); + if (fd < 0) + { + continue; + } + + entry_mod_fdset_linux(fd, i, inx, entry, readfd, writefd, exceptfd); + } +} + +i32 select_thread_set_fdset_linux(i32 nfds, + fd_set * readfd, + fd_set * writefd, + fd_set * exceptfd, + struct select_module_info *module, + i32 inx, i32 err) +{ + + struct select_entry *tmp; + + if (!module) + { + return FALSE; + } + + select_spin_lock(&module->lock); + for (tmp = module->entry_head; NULL != tmp; tmp = tmp->next) + { + if (tmp->cb[inx].count <= 0) + { + continue; + } + + if (nfds < 0) + { + tmp->ready.readyset = nfds; + tmp->ready.select_errno = err; + continue; + } + NSSOC_LOGDBG("readyset=%d,index=%d", tmp->ready.readyset, inx); + entry_module_fdset_linux(tmp, nfds, readfd, writefd, exceptfd, inx); + } + select_spin_unlock(&module->lock); + return TRUE; + +} + +/*no need to check null pointer*/ /***************************************************************************** * Prototype : select_event_post -* Description : when event ready post sem to awake nstack_select +* Description : when event ready post sem to awaik nstack_select * Input : struct select_module_info *module * Output : None * Return Value : void * Calls : * Called By : *****************************************************************************/ -void -select_event_post (struct select_module_info *module) +void select_event_post(struct select_module_info *module) { - struct select_entry *tmp; - int inx; - select_spin_lock (&module->lock); - for (tmp = module->entry_head; NULL != tmp; tmp = tmp->next) + struct select_entry *tmp; + int inx; + select_spin_lock(&module->lock); + for (tmp = module->entry_head; NULL != tmp; tmp = tmp->next) { - if ((tmp->ready.readyset != 0)) + if ((tmp->ready.readyset != 0)) { - for (inx = 0; inx < get_mode_num (); inx++) + for (inx = 0; inx < nstack_get_module_num(); inx++) { - tmp->cb[inx].count = 0; + tmp->cb[inx].count = 0; } - NSSOC_LOGDBG ("readyset=%d", tmp->ready.readyset); - select_sem_post (&tmp->sem); + NSSOC_LOGDBG("readyset=%d", tmp->ready.readyset); + select_sem_post(&tmp->sem); /*do not need return value */ } } - select_spin_unlock (&module->lock); + select_spin_unlock(&module->lock); } +/*no need to check null pointer*/ + /*set select_event function*/ -/***************************************************************************** -* Prototype : select_module_init -* Description : init select module -* Input : None -* Output : None -* Return Value : i32 -* Calls : -* Called By : -*****************************************************************************/ -i32 -select_module_init () +i32 select_module_init() { - i32 i; - pthread_t select_thread_id; - i32 retval; + i32 i; + i32 retval; - if (fdmapping_init () < 0) + if (fdmapping_init() < 0) { - goto ERR_RET; + goto ERR_RET; } - g_select_module.default_mod = get_mode_linux_index (); - g_select_module.default_fun = nsfw_base_select; + g_select_module.default_mod = nstack_get_linux_mid(); + g_select_module.default_fun = nsfw_base_select; - /*regist select fun */ - for (i = 0; i < get_mode_num (); i++) + /*regist select fun */ + for (i = 0; i < nstack_get_module_num(); i++) { - g_select_module.get_select_fun_nonblock[i] = - nstack_module_ops (i)->pfselect; + g_select_module.get_select_fun_nonblock[i] = + nstack_module_ops(i)->pfselect; } - select_sem_init (&g_select_module.sem, 0, 0); - select_spin_lock_init (&g_select_module.lock); + select_sem_init(&g_select_module.sem, 0, 0); /*do not need return value */ + select_spin_lock_init(&g_select_module.lock); - if (pthread_create (&select_thread_id, NULL, nstack_select_thread, NULL)) + if (pthread_create(&g_select_thread_id, NULL, nstack_select_thread, NULL)) { - goto ERR_RET; + goto ERR_RET; } - retval = pthread_setname_np (select_thread_id, "nstack_select"); - if (retval) + retval = pthread_setname_np(g_select_thread_id, "nstack_select"); + if (retval) { - /*set thread name failed */ + /*set thread name failed */ } - g_select_module.inited = TRUE; - g_select_module.entry_head = g_select_module.entry_tail = NULL; - return TRUE; + g_select_module.inited = TRUE; + g_select_module.entry_head = g_select_module.entry_tail = NULL; + return TRUE; -ERR_RET: + ERR_RET: - return FALSE; + return FALSE; } -/***************************************************************************** -* Prototype : entry_module_fdset -* Description : set event -* Input : struct select_entry *entry -* i32 fd_size -* nstack_fd_set *readfd -* nstack_fd_set *writefd -* nstack_fd_set *exceptfd -* i32 inx -* Output : None -* Return Value : void -* Calls : -* Called By : -*****************************************************************************/ -void -entry_module_fdset (struct select_entry *entry, - i32 fd_size, - nstack_fd_set * readfd, - nstack_fd_set * writefd, - nstack_fd_set * exceptfd, i32 inx) +NSTACK_STATIC inline void entry_mod_fdset_nstack(int fd, int idx, int inx, + struct select_entry *entry, + nstack_fd_set * readfd, + nstack_fd_set * writefd, + nstack_fd_set * exceptfd) { - i32 i; - i32 fd; + if (NSTACK_FD_ISSET(idx, readfd) + && NSTACK_FD_ISSET(idx, &entry->cb[inx].nstack_readset)) + { + FD_SET(fd, &entry->ready.readset); + entry->ready.count++; + NSSOC_LOGDBG("readyset is %d", entry->ready.readyset); + } - for (i = 0; i < fd_size; i++) + if (NSTACK_FD_ISSET(idx, writefd) + && NSTACK_FD_ISSET(idx, &entry->cb[inx].nstack_writeset)) { - fd = select_get_commfd (i, inx); - if (fd < 0) - { - continue; - } - if (NSTACK_FD_ISSET (i, readfd) - && NSTACK_FD_ISSET (i, &entry->cb[inx].nstack_readset)) - { - FD_SET (fd, &entry->ready.readset); - entry->ready.readyset++; - NSSOC_LOGDBG ("readyset is %d", entry->ready.readyset); - } + FD_SET(fd, &entry->ready.writeset); + entry->ready.count++; + NSSOC_LOGDBG("writeset is %d", entry->ready.readyset); + } - if (NSTACK_FD_ISSET (i, writefd) - && NSTACK_FD_ISSET (i, &entry->cb[inx].nstack_writeset)) - { - FD_SET (fd, &entry->ready.writeset); - entry->ready.readyset++; - NSSOC_LOGDBG ("writeset is %d", entry->ready.readyset); - } + if (NSTACK_FD_ISSET(idx, exceptfd) + && NSTACK_FD_ISSET(idx, &entry->cb[inx].nstack_exceptset)) + { + FD_SET(fd, &entry->ready.exceptset); + entry->ready.count++; + NSSOC_LOGDBG("exceptset is %d", entry->ready.readyset); + } +} - if (NSTACK_FD_ISSET (i, exceptfd) - && NSTACK_FD_ISSET (i, &entry->cb[inx].nstack_exceptset)) +/*no need to check null pointer*/ +void entry_module_fdset(struct select_entry *entry, + i32 fd_size, + nstack_fd_set * readfd, + nstack_fd_set * writefd, + nstack_fd_set * exceptfd, i32 inx) +{ + i32 i; + i32 fd; + + for (i = 0; i < fd_size; i++) + { + fd = select_get_commfd(i, inx); + if (fd < 0) { - FD_SET (fd, &entry->ready.exceptset); - entry->ready.readyset++; - NSSOC_LOGDBG ("exceptset is %d", entry->ready.readyset); + continue; } + + entry_mod_fdset_nstack(fd, i, inx, entry, readfd, writefd, exceptfd); } +} +NSTACK_STATIC inline int nstack_fd_copy(nstack_fd_set * psrc, + nstack_fd_set * pdst, u32 size) +{ + return memcpy_s(pdst->fds_bits, size, psrc->fds_bits, size); +} + +NSTACK_STATIC inline int alloc_and_init_fd_set(nstack_fd_set * readfd, + nstack_fd_set * writefd, + nstack_fd_set * exceptfd, + struct select_cb_p *select_cb) +{ + int ret = 0; + u32 fds_bits_size = + sizeof(unsigned char) * ((NSTACK_SELECT_MAX_FD + 7) >> 3); + + readfd->fds_bits = select_fd_set_bits_alloc(); + writefd->fds_bits = select_fd_set_bits_alloc(); + exceptfd->fds_bits = select_fd_set_bits_alloc(); + if (!readfd->fds_bits || !writefd->fds_bits || !exceptfd->fds_bits) + { + return -1; + } + + ret |= + nstack_fd_copy(&(select_cb->nstack_readset), readfd, fds_bits_size); + ret |= + nstack_fd_copy(&(select_cb->nstack_writeset), writefd, fds_bits_size); + ret |= + nstack_fd_copy(&(select_cb->nstack_exceptset), exceptfd, + fds_bits_size); + if (EOK != ret) + { + return -1; + } + + return 0; +} + +NSTACK_STATIC inline i32 select_scan_linux(struct select_entry * entry, + int inx) +{ + i32 fd_size; + i32 ready; + fd_set readfd; + fd_set writefd; + fd_set exceptfd; + struct timeval timeout; + + fd_size = entry->cb[inx].count; + if (!g_select_module.get_select_fun_nonblock[inx] || (fd_size <= 0)) + { + return TRUE; + } + + readfd = (entry->cb[inx].readset); + writefd = (entry->cb[inx].writeset); + exceptfd = (entry->cb[inx].exceptset); + + timeout.tv_sec = 0; + timeout.tv_usec = 0; + + ready = + g_select_module.get_select_fun_nonblock[inx] (fd_size, &readfd, + &writefd, &exceptfd, + &timeout); + if (ready > 0) + { + entry_module_fdset_linux(entry, fd_size, &readfd, &writefd, + &exceptfd, inx); + } + else if (ready < 0) + { + entry->ready.count = ready; + entry->ready.select_errno = errno; + NSSOC_LOGERR("select failed index = %d", inx); + return FALSE; + } + + return TRUE; +} + +NSTACK_STATIC inline i32 select_scan_nstack(struct select_entry * entry, + int inx) +{ + i32 fd_size; + i32 ready; + i32 ret = TRUE; + nstack_fd_set *readfd = NULL; + nstack_fd_set *writefd = NULL; + nstack_fd_set *exceptfd = NULL; + struct timeval timeout; + + fd_size = entry->cb[inx].count; + if (!g_select_module.get_select_fun_nonblock[inx] || (fd_size <= 0)) + { + return TRUE; + } + + readfd = select_alloc(sizeof(nstack_fd_set)); + writefd = select_alloc(sizeof(nstack_fd_set)); + exceptfd = select_alloc(sizeof(nstack_fd_set)); + + if (!readfd || !writefd || !exceptfd) + { + NSSOC_LOGERR("malloc fd sets failed"); + FREE_SELECT_FD_SET(readfd, writefd, exceptfd); + return FALSE; + } + + if (alloc_and_init_fd_set(readfd, writefd, exceptfd, &(entry->cb[inx]))) + { + NSSOC_LOGERR("malloc fd bits failed"); + goto return_over; + ret = FALSE; + } + + timeout.tv_sec = 0; + timeout.tv_usec = 0; + + ready = + g_select_module.get_select_fun_nonblock[inx] (fd_size, + (fd_set *) readfd, + (fd_set *) writefd, + (fd_set *) exceptfd, + &timeout); + if (ready > 0) + { + entry_module_fdset(entry, fd_size, readfd, writefd, exceptfd, inx); + } + else if (ready < 0) + { + entry->ready.count = ready; + entry->ready.select_errno = errno; + NSSOC_LOGERR("select failed index = %d", inx); + goto return_over; + ret = FALSE; + } + + return_over: + SELECT_FREE_FD_BITS(readfd, writefd, exceptfd); + FREE_SELECT_FD_SET(readfd, writefd, exceptfd); + return ret; } /***************************************************************************** @@ -496,100 +786,37 @@ entry_module_fdset (struct select_entry *entry, * Calls : * Called By : *****************************************************************************/ -i32 -select_scan (struct select_entry *entry) +i32 select_scan(struct select_entry * entry) { - i32 inx; - i32 fd_size; - i32 ready; - nstack_fd_set *readfd; - nstack_fd_set *writefd; - nstack_fd_set *exceptfd; - struct timeval timeout; - - readfd = malloc (sizeof (nstack_fd_set)); - writefd = malloc (sizeof (nstack_fd_set)); - exceptfd = malloc (sizeof (nstack_fd_set)); - if ((!readfd) || (!writefd) || (!exceptfd)) - { - NSPOL_LOGERR ("malloc nstack_fd_set fail"); - FREE_FD_SET (readfd, writefd, exceptfd); - return -1; - } - for (inx = 0; inx < get_mode_num (); inx++) - { - - *readfd = entry->cb[inx].nstack_readset; - *writefd = entry->cb[inx].nstack_writeset; - *exceptfd = entry->cb[inx].nstack_exceptset; - fd_size = entry->cb[inx].count; - timeout.tv_sec = 0; - timeout.tv_usec = 0; - if ((g_select_module.get_select_fun_nonblock[inx]) && (fd_size > 0)) + i32 inx; + int ret = 0; + + for (inx = 0; inx < nstack_get_module_num(); inx++) + { + if (inx == nstack_get_linux_mid()) { - ready = - g_select_module.get_select_fun_nonblock[inx] (fd_size, - (fd_set *) readfd, - (fd_set *) writefd, - (fd_set *) exceptfd, - &timeout); + ret = select_scan_linux(entry, inx); } - else + else { - continue; + ret = select_scan_nstack(entry, inx); } - if (ready > 0) + if (!ret) { - entry_module_fdset (entry, fd_size, readfd, writefd, exceptfd, inx); + return FALSE; } - else if (ready < 0) - { - entry->ready.readyset = ready; - entry->ready.select_errno = errno; - NSSOC_LOGERR ("select failed index = %d", inx); - FREE_FD_SET (readfd, writefd, exceptfd); - return FALSE; - } - } - FREE_FD_SET (readfd, writefd, exceptfd); - return TRUE; -} -/***************************************************************************** -* Prototype : lint_lock -* Description : avoid lint error -* Input : None -* Output : None -* Return Value : static inline void -* Calls : -* Called By : -*****************************************************************************/ -static inline void -lint_lock () -{ - return; + return TRUE; } -/***************************************************************************** -* Prototype : lint_unlock -* Description : avoid lint error -* Input : None -* Output : None -* Return Value : static inline void -* Calls : -* Called By : -*****************************************************************************/ -static inline void -lint_unlock () -{ - return; -} +/*no need to check null pointer*/ +/*try to get event form all modules */ /***************************************************************************** * Prototype : nstack_select_thread -* Description : if global list not null scaning all modules ,need to think +* Description : if gloab list not null scaning all modules ,need to think about block mod * Input : void *arg * Output : None @@ -597,100 +824,145 @@ lint_unlock () * Calls : * Called By : *****************************************************************************/ -void * -nstack_select_thread (void *arg) -{ - -#define SELECT_SLEEP_TIME 100 //us - i32 inx; - nstack_fd_set *readfd; - nstack_fd_set *writefd; - nstack_fd_set *exceptfd; - i32 fd_size; - i32 ready; - i32 sleep_time = SELECT_SLEEP_TIME; - struct timeval timeout; +void *nstack_select_thread(void *arg) +{ - lint_lock (); +#define SELECT_SLEEP_TIME 800 //us + + i32 inx; + nstack_fd_set *readfd; + nstack_fd_set *writefd; + nstack_fd_set *exceptfd; + fd_set rdfd; + fd_set wtfd; + fd_set expfd; + i32 fd_size; + i32 ready; + i32 sleep_time = SELECT_SLEEP_TIME; + struct timeval timeout; + int selet_errno; + + readfd = select_alloc(sizeof(nstack_fd_set)); + writefd = select_alloc(sizeof(nstack_fd_set)); + exceptfd = select_alloc(sizeof(nstack_fd_set)); + if ((!readfd) || (!writefd) || (!exceptfd)) + { + NSPOL_LOGERR("malloc nstack_fd_set fail"); + FREE_SELECT_FD_SET(readfd, writefd, exceptfd); + return NULL; + } - readfd = malloc (sizeof (nstack_fd_set)); - writefd = malloc (sizeof (nstack_fd_set)); - exceptfd = malloc (sizeof (nstack_fd_set)); - if ((!readfd) || (!writefd) || (!exceptfd)) + readfd->fds_bits = select_fd_set_bits_alloc(); + writefd->fds_bits = select_fd_set_bits_alloc(); + exceptfd->fds_bits = select_fd_set_bits_alloc(); + if ((!readfd->fds_bits) || (!writefd->fds_bits) || (!exceptfd->fds_bits)) { - NSPOL_LOGERR ("malloc nstack_fd_set fail"); - FREE_FD_SET (readfd, writefd, exceptfd); - lint_unlock (); - return NULL; + NSPOL_LOGERR("malloc fd_bits for nstack_fd_set fail"); + + SELECT_FREE_FD_BITS(readfd, writefd, exceptfd); + FREE_SELECT_FD_SET(readfd, writefd, exceptfd); + return NULL; } - /*used nonblock need add block mod later */ + /*used nonblock need add block mod later */ - for (;;) + for (;;) { - /*wait app calling select no cong cpu */ - if (!g_select_module.entry_head) + /*wait app calling select no cong cpu */ + if (!g_select_module.entry_head) { - select_sem_wait (&g_select_module.sem); + select_sem_wait(&g_select_module.sem); /*do not need return value */ } - for (inx = 0; inx < get_mode_num (); inx++) + for (inx = 0; inx < nstack_get_module_num(); inx++) { - fd_size = - select_thread_get_fdset (readfd, writefd, exceptfd, - &g_select_module, inx); - if (fd_size <= 0) + if (inx == nstack_get_linux_mid()) { - continue; - } + fd_size = + select_thread_get_fdset_linux(&rdfd, &wtfd, &expfd, + &g_select_module, inx); + if (fd_size <= 0) + continue; + if (g_select_module.get_select_fun_nonblock[inx]) + ready = + g_select_module.get_select_fun_nonblock[inx] (fd_size, + (fd_set + *) & + rdfd, + (fd_set + *) & + wtfd, + (fd_set + *) & + expfd, + &timeout); + else + continue; + + if (ready > 0) + + select_thread_set_fdset_linux(fd_size, &rdfd, + &wtfd, &expfd, + &g_select_module, inx, 0); + else if (ready < 0) + { - if (g_select_module.get_select_fun_nonblock[inx]) - { - ready = - g_select_module.get_select_fun_nonblock[inx] (fd_size, - (fd_set *) - readfd, - (fd_set *) - writefd, - (fd_set *) - exceptfd, - &timeout); + selet_errno = errno; + select_thread_set_fdset_linux(fd_size, &rdfd, &wtfd, + &expfd, &g_select_module, + inx, selet_errno); + break; + } } - else + else { - continue; - } + fd_size = + select_thread_get_fdset(readfd, writefd, exceptfd, + &g_select_module, inx); + if (fd_size <= 0) + continue; + if (g_select_module.get_select_fun_nonblock[inx]) + ready = + g_select_module.get_select_fun_nonblock[inx] (fd_size, + (fd_set + *) + readfd, + (fd_set + *) + writefd, + (fd_set + *) + exceptfd, + &timeout); + else + continue; + + if (ready > 0) + select_thread_set_fdset(fd_size, readfd, writefd, exceptfd, &g_select_module, inx, 0); /*do not need return value */ + else if (ready < 0) + { + selet_errno = errno; + select_thread_set_fdset(ready, readfd, writefd, exceptfd, &g_select_module, inx, selet_errno); /*do not need return value */ + break; + } - if (ready > 0) - { - select_thread_set_fdset (fd_size, readfd, writefd, exceptfd, - &g_select_module, inx, 0); - } - else if (ready < 0) - { - select_thread_set_fdset (ready, readfd, writefd, exceptfd, - &g_select_module, inx, errno); - NSSOC_LOGERR ("module[%d] select failed] ret = %d errno = %d", - inx, ready, errno); - lint_unlock (); - break; } } - select_event_post (&g_select_module); - timeout.tv_sec = 0; - timeout.tv_usec = sleep_time; - lint_unlock (); - /*use linux select for timer */ - nsfw_base_select (1, NULL, NULL, NULL, &timeout); + select_event_post(&g_select_module); + timeout.tv_sec = 0; + timeout.tv_usec = sleep_time; + /*use linux select for timer */ + nsfw_base_select(1, NULL, NULL, NULL, &timeout); + //sys_sleep_ns(0, sleep_time); //g_sem_sleep_time } } /***************************************************************************** * Prototype : nssct_create -* Description : create a select record for event fd +* Description : create a select record for eveny fd * Input : i32 cfd * i32 mfd * i32 inx @@ -699,15 +971,14 @@ nstack_select_thread (void *arg) * Calls : * Called By : *****************************************************************************/ -void -nssct_create (i32 cfd, i32 mfd, i32 inx) +void nssct_create(i32 cfd, i32 mfd, i32 inx) { - if (g_select_module.inited != TRUE) + if (g_select_module.inited != TRUE) { - return; + return; } - select_set_modfd (cfd, inx, mfd); - select_set_commfd (mfd, inx, cfd); + select_set_modfd(cfd, inx, mfd); /*do not need return value */ + select_set_commfd(mfd, inx, cfd); /*do not need return value */ } /***************************************************************************** @@ -720,17 +991,16 @@ nssct_create (i32 cfd, i32 mfd, i32 inx) * Calls : * Called By : *****************************************************************************/ -void -nssct_close (i32 cfd, i32 inx) +void nssct_close(i32 cfd, i32 inx) { - if (g_select_module.inited != TRUE) + if (g_select_module.inited != TRUE) { - return; + return; } - i32 mfd = select_get_modfd (cfd, inx); - select_set_modfd (cfd, inx, -1); - select_set_commfd (mfd, inx, -1); - select_set_index (cfd, -1); + i32 mfd = select_get_modfd(cfd, inx); + select_set_modfd(cfd, inx, -1); /*do not need return value */ + select_set_commfd(mfd, inx, -1); /*do not need return value */ + select_set_index(cfd, -1); /*do not need return value */ } /***************************************************************************** @@ -743,36 +1013,260 @@ nssct_close (i32 cfd, i32 inx) * Calls : * Called By : *****************************************************************************/ -void -nssct_set_index (i32 fd, i32 inx) +void nssct_set_index(i32 fd, i32 inx) +{ + if (g_select_module.inited != TRUE) + { + return; + } + select_set_index(fd, inx); /*do not need return value */ +} + +int select_scan_return_from_entry(fd_set * readfds, fd_set * writefds, + fd_set * exceptfds, + struct select_entry *entry) +{ + int ret; + if (readfds) + { + *readfds = entry->ready.readset; + } + if (writefds) + { + *writefds = entry->ready.writeset; + } + if (exceptfds) + { + *exceptfds = entry->ready.exceptset; + } + + ret = entry->ready.readyset; + if (ret < 0) + { + errno = entry->ready.select_errno; + } + return ret; +} + +void nstack_select_entry_free(struct select_entry *entry) +{ + int i; + + if (!entry) + return; + + for (i = 0; i < nstack_get_module_num(); i++) + { + + SELECT_FREE_FD_BITS(&entry->cb[i].nstack_readset, + &entry->cb[i].nstack_writeset, + &entry->cb[i].nstack_exceptset); + } + + SELECT_FREE_FD_BITS(&entry->ready.nstack_readset, + &entry->ready.nstack_writeset, + &entry->ready.nstack_exceptset); + +} + +void nstack_select_entry_alloc(struct select_entry **entry) { - if (g_select_module.inited != TRUE) + struct select_entry *tmp; + int i; + + tmp = select_alloc(sizeof(struct select_entry)); + if (!tmp) + return; + for (i = 0; i < nstack_get_module_num(); i++) + { + tmp->cb[i].nstack_readset.fds_bits = select_fd_set_bits_alloc(); + tmp->cb[i].nstack_writeset.fds_bits = select_fd_set_bits_alloc(); + tmp->cb[i].nstack_exceptset.fds_bits = select_fd_set_bits_alloc(); + if (!tmp->cb[i].nstack_readset.fds_bits || + !tmp->cb[i].nstack_writeset.fds_bits || + !tmp->cb[i].nstack_exceptset.fds_bits) + { + goto err_return; + } + } + + tmp->ready.nstack_readset.fds_bits = select_fd_set_bits_alloc(); + tmp->ready.nstack_writeset.fds_bits = select_fd_set_bits_alloc(); + tmp->ready.nstack_exceptset.fds_bits = select_fd_set_bits_alloc(); + if (!tmp->ready.nstack_readset.fds_bits || + !tmp->ready.nstack_writeset.fds_bits || + !tmp->ready.nstack_exceptset.fds_bits) { - return; + goto err_return; + } + + *entry = tmp; + return; + err_return: + nstack_select_entry_free(tmp); + *entry = NULL; +} + +void select_fail_stat(i32 nfds, + fd_set * readfd, fd_set * writefd, fd_set * exceptfd) +{ + i32 i; + i32 event_id = 0; + nstack_fd_Inf *fdInf = NULL; + + for (i = 0; i < nfds; i++) + { + + event_id = 0; + if (!((readfd && FD_ISSET(i, readfd)) || + (writefd && FD_ISSET(i, writefd)) || + (exceptfd && FD_ISSET(i, exceptfd)))) + { + continue; + } + + fdInf = nstack_get_valid_inf(i); + if ((NULL == fdInf) || !fdInf->ops + || (fdInf->rmidx != MOD_INDEX_FOR_STACKPOOL)) + { + continue; + } + if ((readfd) && (FD_ISSET(i, readfd))) + { + event_id |= EPOLLIN; + } + if ((writefd) && (FD_ISSET(i, writefd))) + { + event_id |= EPOLLOUT; + } + if ((exceptfd) && (FD_ISSET(i, exceptfd))) + { + event_id |= EPOLLERR; + } + nstack_dfx_state_update((u64) fdInf->rlfd, fdInf->rmidx, + DMM_APP_SELECT_FAIL, + (void *) ((u64_t) event_id)); } - select_set_index (fd, inx); } -i32 -select_module_init_child () +int nstack_select_processing(int nfds, fd_set * readfds, fd_set * writefds, + fd_set * exceptfds, struct timeval *timeout) { - pthread_t select_thread_id; - i32 retval; + int ret = -1; + struct select_module_info *select_module = get_select_module(); + struct select_entry *entry = NULL; - if (pthread_create (&select_thread_id, NULL, nstack_select_thread, NULL)) + nstack_select_entry_alloc(&entry); + if (NULL == entry) { - goto ERR_RET; + errno = ENOMEM; + NSSOC_LOGERR("select entry alloc fail"); + goto err_return; } + /* need init sem */ + select_sem_init(&entry->sem, 0, 0); /*do not need return value */ + + /* fix dead-code type */ + /*split select fd to each modules fd and save to entry */ + (void) select_cb_split_by_mod(nfds, readfds, writefds, exceptfds, entry); + + /*if all fd in default module we just calling it */ + if (entry->info.set_num <= 1) + { + + /*adapte linux */ + if ((select_module) + && (entry->info.index == select_module->default_mod)) + { + if (select_module->default_fun) + { + ret = + select_module->default_fun(nfds, readfds, writefds, + exceptfds, timeout); + } + else + { + ret = + nsfw_base_select(nfds, readfds, writefds, exceptfds, + timeout); + } + goto err_return; + } + } + + /*cheching if event ready or not */ + if (FALSE == select_scan(entry)) + { + NSSOC_LOGERR("select scan fail"); + goto err_return; + } + + if (entry->ready.readyset != 0) + { + goto scan_return; + } + + if (TIMEVAL_EQUAL_ZERO(timeout)) + { + goto scan_return; + } + + if (FALSE == select_add_cb(entry)) + { + errno = ENOMEM; + NSSOC_LOGERR("select entry add fail"); + goto err_return; + } + + if (NULL == timeout) + { + select_sem_wait(&entry->sem); /*do not need return value */ + } + else + { + long time_cost = 0; + long msec = 0; + if (nstack_timeval2msec(timeout, &msec)) + { + nstack_set_errno(EINVAL); + goto err_return; + } + ret = nstack_sem_timedwait(&entry->sem, msec, &time_cost); + if (ret < 0) + { + nstack_set_errno(EINVAL); + goto err_return; + } + + if (time_cost >= msec) + { + select_fail_stat(nfds, readfds, writefds, exceptfds); + timeout->tv_sec = 0; + timeout->tv_usec = 0; + } + else if (time_cost > 0) + { + msec = msec - time_cost; + timeout->tv_sec = msec / 1000; + timeout->tv_usec = (msec % 1000) * 1000; + } + } + + select_rm_cb(entry); /*do not need return value */ + + scan_return: + ret = select_scan_return_from_entry(readfds, writefds, exceptfds, entry); - retval = pthread_setname_np (select_thread_id, "nstack_select_child"); - if (retval) + err_return: + if (entry) { - /*set thread name failed */ + nstack_select_entry_free(entry); } - return TRUE; + NSSOC_LOGDBG + ("nfds=%d,readfds=%p,writefds=%p,exceptfds=%p,timeout=%p,ret=%d errno=%d", + nfds, readfds, writefds, exceptfds, timeout, ret, errno); -ERR_RET: - return FALSE; + return ret; } #endif /* NSTACK_SELECT_MODULE */ diff --git a/src/nSocket/nstack/event/select/select_adapt.c b/src/nSocket/nstack/event/select/select_adapt.c index a9858e8..f61d7c1 100644 --- a/src/nSocket/nstack/event/select/select_adapt.c +++ b/src/nSocket/nstack/event/select/select_adapt.c @@ -26,314 +26,199 @@ /*==============================================* * project-wide global variables * *----------------------------------------------*/ -i32 nstack_mod_fd[NSTACK_MAX_MODULE_NUM][NSTACK_SELECT_MAX_FD]; struct select_fd_map_inf g_select_fd_map; /*==============================================* * routines' or functions' implementations * *----------------------------------------------*/ -/***************************************************************************** -* Prototype : select_alloc -* Description : select_alloc -* Input : int size -* Output : None -* Return Value : void * -* Calls : -* Called By : -*****************************************************************************/ -void * -select_alloc (int size) +void *select_alloc(int size) { - char *p; - if (size <= 0) + char *p; + if (size <= 0) { - return NULL; + return NULL; } - p = malloc (size); - if (!p) + p = malloc(size); + if (!p) { - return NULL; + return NULL; } - if (EOK != MEMSET_S (p, size, 0, size)) + if (EOK != memset_s(p, size, 0, size)) { - free (p); - p = NULL; + free(p); + p = NULL; } - return p; + return p; } -/***************************************************************************** -* Prototype : select_free -* Description : select_free -* Input : char *p -* Output : None -* Return Value : void -* Calls : -* Called By : -*****************************************************************************/ -void -select_free (char *p) +/*point is set to NULL because it's freeed */ +void select_free(void *p) { - if (p) + if (p) { - free (p); - p = NULL; + free(p); + p = NULL; } } -/***************************************************************************** -* Prototype : get_select_fdinf -* Description : get_select_fdinf -* Input : i32 fd -* Output : None -* Return Value : struct select_comm_fd_map * -* Calls : -* Called By : -*****************************************************************************/ -struct select_comm_fd_map * -get_select_fdinf (i32 fd) +struct select_comm_fd_map *get_select_fdinf(i32 fd) { - if ((fd < 0) || (fd >= NSTACK_SELECT_MAX_FD)) + if ((fd < 0) || ((u32) fd >= NSTACK_SELECT_MAX_FD)) { - return NULL; + return NULL; } - return (&g_select_fd_map.fdinf[fd]); + return (&g_select_fd_map.fdinf[fd]); } -/***************************************************************************** -* Prototype : reset_select_fdinf -* Description : reset_select_fdinf -* Input : i32 fd -* Output : None -* Return Value : void -* Calls : -* Called By : -*****************************************************************************/ -void -reset_select_fdinf (i32 fd) +void reset_select_fdinf(i32 fd) { - i32 i; - struct select_comm_fd_map *fdinf = get_select_fdinf (fd); - if (NULL == fdinf) + i32 i; + struct select_comm_fd_map *fdinf = get_select_fdinf(fd); + /* fdinf is possible is null */ + if (NULL == fdinf) { - return; + return; } - fdinf->index = -1; - for (i = 0; i < NSTACK_MAX_MODULE_NUM; i++) + fdinf->index = -1; + for (i = 0; i < NSTACK_MAX_MODULE_NUM; i++) { - fdinf->mod_fd[i] = -1; + fdinf->mod_fd[i] = -1; } } -/***************************************************************************** -* Prototype : select_get_modfd -* Description : select_get_modfd -* Input : i32 fd -* i32 inx -* Output : None -* Return Value : i32 -* Calls : -* Called By : -*****************************************************************************/ -i32 -select_get_modfd (i32 fd, i32 inx) +i32 select_get_modfd(i32 fd, i32 inx) { - if ((fd < 0) || (fd >= NSTACK_SELECT_MAX_FD)) + if ((fd < 0) || ((u32) fd >= NSTACK_SELECT_MAX_FD)) { - return -1; + return -1; } - if ((inx < 0)) + if ((inx < 0)) { - return -1; + return -1; } - if (!g_select_fd_map.fdinf) + if (!g_select_fd_map.fdinf) { - return FALSE; + return FALSE; } - return (g_select_fd_map.fdinf[fd].mod_fd[inx]); + return (g_select_fd_map.fdinf[fd].mod_fd[inx]); } -/***************************************************************************** -* Prototype : select_set_modfd -* Description : select_set_modfd -* Input : i32 fd -* i32 inx -* i32 modfd -* Output : None -* Return Value : i32 -* Calls : -* Called By : -*****************************************************************************/ -i32 -select_set_modfd (i32 fd, i32 inx, i32 modfd) +i32 select_set_modfd(i32 fd, i32 inx, i32 modfd) { - if ((fd < 0) || (fd >= NSTACK_SELECT_MAX_FD)) + if ((fd < 0) || ((u32) fd >= NSTACK_SELECT_MAX_FD)) { - return -1; + return -1; } - if (!g_select_fd_map.fdinf) + if (!g_select_fd_map.fdinf) { - return FALSE; + return FALSE; } - g_select_fd_map.fdinf[fd].mod_fd[inx] = modfd; + g_select_fd_map.fdinf[fd].mod_fd[inx] = modfd; - return TRUE; + return TRUE; } -/***************************************************************************** -* Prototype : select_get_modindex -* Description : select_get_modindex -* Input : i32 fd -* Output : None -* Return Value : i32 -* Calls : -* Called By : -*****************************************************************************/ -i32 -select_get_modindex (i32 fd) +i32 select_get_modindex(i32 fd) { - if ((fd < 0) || (fd >= NSTACK_SELECT_MAX_FD)) + if ((fd < 0) || ((u32) fd >= NSTACK_SELECT_MAX_FD)) { - return -1; + return -1; } - return g_select_fd_map.fdinf[fd].index; + return g_select_fd_map.fdinf[fd].index; } -/***************************************************************************** -* Prototype : select_get_commfd -* Description : select_get_commfd -* Input : i32 modfd -* i32 inx -* Output : None -* Return Value : i32 -* Calls : -* Called By : -*****************************************************************************/ -i32 -select_get_commfd (i32 modfd, i32 inx) +i32 select_get_commfd(i32 modfd, i32 inx) { - if ((modfd < 0) || (modfd >= NSTACK_SELECT_MAX_FD)) + if ((modfd < 0) || ((u32) modfd >= NSTACK_SELECT_MAX_FD)) { - return -1; + return -1; } - return g_select_fd_map.modinf[inx].comm_fd[modfd]; + return g_select_fd_map.modinf[inx].comm_fd[modfd]; } -/***************************************************************************** -* Prototype : select_set_commfd -* Description : select_set_commfd -* Input : i32 modfd -* i32 inx -* i32 fd -* Output : None -* Return Value : i32 -* Calls : -* Called By : -*****************************************************************************/ -i32 -select_set_commfd (i32 modfd, i32 inx, i32 fd) +i32 select_set_commfd(i32 modfd, i32 inx, i32 fd) { - if ((modfd < 0) || (modfd >= NSTACK_SELECT_MAX_FD)) + if ((modfd < 0) || ((u32) modfd >= NSTACK_SELECT_MAX_FD)) { - return -1; + return -1; } - if (!g_select_fd_map.modinf[inx].comm_fd) + if (!g_select_fd_map.modinf[inx].comm_fd) { - return FALSE; + return FALSE; } - g_select_fd_map.modinf[inx].comm_fd[modfd] = fd; + g_select_fd_map.modinf[inx].comm_fd[modfd] = fd; - return TRUE; + return TRUE; } -/***************************************************************************** -* Prototype : select_set_index -* Description : select_set_index -* Input : i32 fd -* i32 inx -* Output : None -* Return Value : i32 -* Calls : -* Called By : -*****************************************************************************/ -i32 -select_set_index (i32 fd, i32 inx) +i32 select_set_index(i32 fd, i32 inx) { - if ((fd < 0) || (fd >= NSTACK_SELECT_MAX_FD)) + if ((fd < 0) || ((u32) fd >= NSTACK_SELECT_MAX_FD)) { - return -1; + return -1; } - if (!g_select_fd_map.fdinf) + if (!g_select_fd_map.fdinf) { - return FALSE; + return FALSE; } - g_select_fd_map.fdinf[fd].index = inx; - return TRUE; + g_select_fd_map.fdinf[fd].index = inx; + return TRUE; } -/***************************************************************************** -* Prototype : fdmapping_init -* Description : fdmapping_init -* Input : void -* Output : None -* Return Value : i32 -* Calls : -* Called By : -*****************************************************************************/ -i32 -fdmapping_init (void) +i32 fdmapping_init(void) { - int ret = FALSE; - int i, inx; + int ret = FALSE; + int i, inx; - g_select_fd_map.fdinf = - (struct select_comm_fd_map *) - select_alloc (sizeof (struct select_comm_fd_map) * NSTACK_SELECT_MAX_FD); - if (NULL == g_select_fd_map.fdinf) + g_select_fd_map.fdinf = + (struct select_comm_fd_map *) + select_alloc(sizeof(struct select_comm_fd_map) * + NSTACK_SELECT_MAX_FD); + if (NULL == g_select_fd_map.fdinf) { - goto err_return; + goto err_return; } - for (i = 0; i < get_mode_num (); i++) + for (i = 0; i < nstack_get_module_num(); i++) { - g_select_fd_map.modinf[i].comm_fd = - (i32 *) select_alloc (sizeof (i32) * NSTACK_SELECT_MAX_FD); - if (NULL == g_select_fd_map.modinf[i].comm_fd) + g_select_fd_map.modinf[i].comm_fd = + (i32 *) select_alloc(sizeof(i32) * NSTACK_SELECT_MAX_FD); + if (NULL == g_select_fd_map.modinf[i].comm_fd) { - goto err_return; + goto err_return; } } - for (i = 0; i < NSTACK_SELECT_MAX_FD; i++) + u32 fd_idx = 0; + for (fd_idx = 0; fd_idx < NSTACK_SELECT_MAX_FD; fd_idx++) { - reset_select_fdinf (i); + reset_select_fdinf(fd_idx); } - for (inx = 0; inx < get_mode_num (); inx++) + for (inx = 0; inx < nstack_get_module_num(); inx++) { - for (i = 0; i < NSTACK_SELECT_MAX_FD; i++) + for (fd_idx = 0; fd_idx < NSTACK_SELECT_MAX_FD; fd_idx++) { - select_set_commfd (i, inx, -1); - + select_set_commfd(fd_idx, inx, -1); } } - ret = TRUE; - return ret; -err_return: + ret = TRUE; + return ret; + err_return: - select_free ((char *) g_select_fd_map.fdinf); - for (i = 0; i < get_mode_num (); i++) + select_free((char *) g_select_fd_map.fdinf); + for (i = 0; i < nstack_get_module_num(); i++) { - select_free ((char *) g_select_fd_map.modinf[i].comm_fd); + select_free((char *) g_select_fd_map.modinf[i].comm_fd); } - return ret; + return ret; } |